我正在尝试向
发帖 http://localhost:8888/test
JS
$('.saveBTN').click(function (event) {
$( "form#editForm" ).on( "submit", function( event ) {
event.preventDefault();
var inputs = {};
$("#editForm :input").each(function() {
inputs[$(this).attr("name")] = $(this).val();
});
var $inputs = JSON.stringify(inputs);
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
url: '/test',
type: 'POST',
dataType: 'json',
data: $inputs,
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log('PUT error.');
}
});
});
<小时/>
我不断得到
500 Internal Server Error
我尝试添加
<meta name="csrf-token" value="{{ csrf_token() }}">
这在我的 Ajax 上
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
对此的任何提示将不胜感激!
最佳答案
将其添加到下面的表单中。
// This will generate token field which will be token
{{ csrf_field() }}
// Expected Output
<input type="hidden" name="_token" value="ssdfdsfsdfsdfs32r23442">
VerifyCsrfToken.php
- 该文件是middleware
,包含在Web中间件组中,会自动验证token
中的token
请求输入与 session 中存储的 token 匹配。
请参阅此 URL 以获取更多信息: http://laravel.com/docs/master/routing#csrf-x-csrf-token
Updates - 23rd Dec 2016
从 Laravel 开始,您也可以像下面这样使用。
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
API引用:https://laravel.com/docs/master/routing#form-method-spoofing
关于php - 500 内部服务器错误 Ajax Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34340304/