Laravel 5 GET 正在运行,但 POST 方法不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37121205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 GET is working but POST method is not working
提问by Zain SMJ
I am new to Laravel and I am facing a weird issue. In the routes I am calling a function through POST and GET method.
我是 Laravel 的新手,我面临着一个奇怪的问题。在路由中,我通过 POST 和 GET 方法调用一个函数。
GET returns data but POST doesn't. Here is my simple code:
GET 返回数据,但 POST 不返回。这是我的简单代码:
For POST
对于 POST
Route::post('register', function() {
echo 'we are here';
});
For GET:
忘记:
Route::get('register', function() {
echo 'we are here';
});
Please help. Thank you.
请帮忙。谢谢你。
回答by Chris
You need to include a CSRF token on every request (except GET).
您需要在每个请求中包含一个 CSRF 令牌(GET 除外)。
回答by Dan Both
Add this to your form:
将此添加到您的表单中:
<input type="hidden" name="_token" value="{{ csrf_token() }}">
Your form will look like this
您的表单将如下所示
<form method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
...
</form>
回答by Vikash Kumar
Laravel automatically generates a CSRF "token" for each active user session managed by the application. This token is used to verify that the authenticated user is the one actually making the requests to the application.
Laravel 会为应用程序管理的每个活动用户会话自动生成一个 CSRF “令牌”。此令牌用于验证经过身份验证的用户是实际向应用程序发出请求的用户。
Anytime you define a HTML form in your application, you should include a hidden CSRF token field in the form so that the CSRF protection middleware will be able to validate the request. To generate a hidden CSRF token field, you may use the csrf_field helper function:
任何时候在应用程序中定义 HTML 表单时,都应该在表单中包含一个隐藏的 CSRF 令牌字段,以便 CSRF 保护中间件能够验证请求。要生成隐藏的 CSRF 令牌字段,您可以使用 csrf_field 辅助函数:
// Vanilla PHP:
// 原生 PHP:
<?php echo csrf_field(); ?>
// Blade Template Syntax:
// 刀片模板语法:
{{ csrf_field() }}
The csrf_field helper function generates the following HTML:
csrf_field 辅助函数生成以下 HTML:
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
You do not need to manually verify the CSRF token on POST, PUT, or DELETE requests. The VerifyCsrfToken middleware, which is included in the web middleware group, will automatically verify that the token in the request input matches the token stored in the session.
您无需在 POST、PUT 或 DELETE 请求上手动验证 CSRF 令牌。包含在 web 中间件组中的 VerifyCsrfToken 中间件将自动验证请求输入中的令牌是否与会话中存储的令牌匹配。
回答by Manu Joseph
If you are working on the web without ajax, you can check out and use the above answers or use
如果您在没有ajax的情况下在网络上工作,您可以查看并使用上述答案或使用
<form action="">
@csrf
...
</form>
If you need to call this URL via ajax request Step 1: Add CSRF token inside your meta tag (), you can specify inside your main.blade.php file (layout)
如果你需要通过ajax请求调用这个URL 第1步:在你的meta标签()中添加CSRF令牌,你可以在你的main.blade.php文件(布局)中指定
<meta name="csrf-token" content="{{ csrf_token() }}">
Step 2: Add X-CSRF-TOKENheader inside your ajax request
第 2 步:在 ajax 请求中添加X-CSRF-TOKEN标头
var APP_URL = {!! json_encode(url('/')) !!}
$.ajax({
type:'post',
data:({search:input}),
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: ''+APP_URL+'/register',
success:function(data)
{
alert("success");
},
error:function(data)
{
alert('error');
},
});
Thank you!
谢谢!