[英]CSRF token mismatch Laravel ajax delete data
I want to delete data from a database with an ajax
call but it's showing an error.我想使用ajax
调用从数据库中删除数据,但它显示错误。
CSRF token mismatch CSRF 令牌不匹配
In header:在 header 中:
<meta name="csrf-token" content="{{ csrf_token() }}">
In blade:在刀片中:
<button class="deletePhoto" data-id="{{ $photo->id }}" data-token="{{ csrf_token() }}">Delete</button>
AJAX call: AJAX 调用:
$('.deletePhoto').click(function () {
var id = $(this).data('id');
var el = this;
$.ajaxSetup({
headers:{
'X_CSRF_TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: '/photo/delete/'+id,
type: 'DELETE',
dataType: 'JSON',
data:{
'id': id,
},
success: function () {
$(el).closest(".photo-details").remove();
console.log('DELETED');
},
error: function (xhr) {
console.log(xhr.responseText);
}
})
})
Controller: Controller:
public function destroy($id)
{
$photo = Photo::find($id);
$photo->delete();
}
This is what I usually do: [AJAX CALL]这是我通常做的: [AJAX CALL]
$.ajax({
url: '/photo/delete/'+id,
type: 'DELETE',
dataType: 'JSON',
data:{
'id': id,
'_token': '{{ csrf_token() }}',
},
success: function () {
el.closest(".photo-details").remove();
console.log('DELETED');
},
error: function (xhr) {
console.log(xhr.responseText);
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.