Jquery 尝试使用Disqs api时出现laravel标记不匹配异常
当有人在Disqs评论线程中向博客帖子发表评论时,我试图向作者发送通知。在laravel中,我尝试在filters.php中过滤ajax调用的csrf令牌和csrf令牌:Jquery 尝试使用Disqs api时出现laravel标记不匹配异常,jquery,session,laravel,token,Jquery,Session,Laravel,Token,当有人在Disqs评论线程中向博客帖子发表评论时,我试图向作者发送通知。在laravel中,我尝试在filters.php中过滤ajax调用的csrf令牌和csrf令牌: Route::filter('csrf_header', function() { if (Session::token() != Request::header('x-csrf-token')) { throw new Illuminate\Session\TokenMismatchExcepti
Route::filter('csrf_header', function()
{
if (Session::token() != Request::header('x-csrf-token'))
{
throw new Illuminate\Session\TokenMismatchException;
}
});
Route::filter('csrf', function()
{
if (Session::token() != Input::get('_token'))
{
throw new Illuminate\Session\TokenMismatchException;//line 126
}
});
当我发表评论时,我得到:
{"error":{"type":"Illuminate\\Session\\TokenMismatchException","message":"","file":"\/home\/ytsejam\/public\/sirtcantalilar.com\/app\/filters.php","line":126}}
我试着发表我的评论如下:
在view-post.blade.php中,我添加了
<meta name="token" content="{{ Session::token() }}">
<script type="text/javascript">
var disqus_config = function() {
this.callbacks.onNewComment = [function(comment) {
$.ajax({
url: '/sendnotification',
type: 'POST',
headers: {
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
},
data: { comment: comment.id, post:{{$post->id}}",author:"$post->author->id"},
dataType: 'JSON',
beforeSend: function(request) {
return request.setRequestHeader("X-CSRF-Token", $("meta[name='token']").attr('content'));
},
success: function(ev) {
},
error: function(xhr, error, status) {
}
});
}];
};
</script>
var discus_config=function(){
this.callbacks.onNewComment=[函数(注释){
$.ajax({
url:“/sendnotification”,
键入:“POST”,
标题:{
'X-CSRF-Token':$('meta[name=“CSRF-Token”]).attr('content'))
},
数据:{comment:comment.id,post:{{$post->id}}”,作者:“$post->author->id”},
数据类型:“JSON”,
发送前:功能(请求){
return request.setRequestHeader(“X-CSRF-Token”,$(“meta[name='Token']”)attr('content');
},
成功:功能(ev){
},
错误:函数(xhr、错误、状态){
}
});
}];
};
你能帮我解决这个问题吗?
谢谢。过了很长一段时间,没有人回答我的问题,你可以根据评论中的要点解决这个问题,其他选项是使用jquery:
$(function() {
$.ajaxSetup({
headers: {
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
}
});
});
var disqus_config = function() {
this.callbacks.onNewComment = [function(comment) {
$.ajax({
url: '/sendnotification',
type: 'POST',
data: { comment: comment.id, post: "{{$post->id}}",author:"{{$author->id}}"},
dataType: 'JSON',
async: true,
success: function(data){
process(data)
}
});
}];
};
以及在头部部分添加csrf令牌:
<meta name="csrf-token" content="<?= csrf_token() ?>">