Ajax删除在Laravel 5.2中不起作用 [英] Ajax delete not working in laravel 5.2
问题描述
我正在尝试在laravel
应用程序的mysql
中记录delete
.但它显示 TokenMismatchException 错误.我无法找到其引发错误的原因.如果有人可以帮忙,那就太好了.
I'm trying to delete
a record in mysql
in laravel
application. But it shows TokenMismatchException error. I am not able to find why its throwing error. It would be great if anyone can help out..
这里的视图代码:(我没有表格)
here's view code: (I don't have a form)
<a href="{{url('/page/'.$Ids->id)}}" class="remove-entry">
<button type="button" class="close" aria-label="Close"><span aria-hidden="true">×</span></button>
</a>
这是 jQuery AJAX调用:
$(document).on('click', '.remove-entry', function(e){
e.preventDefault();
$.ajax({
url: this.href,
type: 'DELETE',
data: {
"_token": "{{ csrf_token() }}"
},
success: function(res) {
console.log('removed');
}
});
});
更新
我厌倦了下面的代码,但是它可能无法工作,因为它需要具有值token
,我认为这是从表单中获取的.但是,正如我上面已经提到的,我没有表单设置.那么在这种情况下如何放置token
值?
I tired the following code but it doesn't work probably because it needs to have value token
which I suppose is to be fetched from form. But as I already mentioned above, I DON'T have a form set-up. So how can I put the token
value in that case?
headers: {'X-CSRF-TOKEN': token}
我了解我所面临的问题不是新问题也不是唯一问题,但它肯定与被认为重复的问题有所不同.此外,该代码之前运行良好,但在新项目中会造成问题
I understand the problem I'm facing is not new or unique, but it surely is different from the questions that it is supposedly considered duplicate of. Also, the code worked fine earlier but its creating issues in new project
推荐答案
如果要在laravel中发送不带CSRF令牌的Ajax请求. 转到:app/Http/Middlewares/VerifyCsrfToken.php
If you want to send Ajax Request without CSRF token in laravel.
Goto : app/Http/Middlewares/VerifyCsrfToken.php
protected $except = [
'request/send_ajax', /// Enter your route post link here
'request/send_ajax1'
];
现在您可以发布不带令牌的Ajax请求
Now you can post ajax request without token
或
在您将要执行Ajax请求的页面上简单插入一个META标记,默认的中间件将检查csrf_token.
Simple insert a META tag on the page where you will do the Ajax request and default middleware will check for csrf_token.
或
$.ajax({
url: "<?php echo url("yourrequest"); ?>",
type: "POST",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data:'column='+column+&id='+id,
success: function(data){
console.log(msg);
}
});
在Laravel5.2上工作
Work on Laravel5.2
这篇关于Ajax删除在Laravel 5.2中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!