【问题标题】:CSRF token error? on laravel Symfony\\Component\\HttpKernel\\Exception\\HttpExceptionCSRF 令牌错误?在 laravel Symfony\\Component\\HttpKernel\\Exception\\HttpException
【发布时间】:2018-09-25 19:15:52
【问题描述】:

first was ok. second got error 我在 laravel 的 javascript 页面上使用 ajax 函数

如果我在它运行良好后启动该功能 但是当我在短时间内启动该功能 2 或 3 次时,我得到了错误

"exception": "Symfony\\Component\\HttpKernel\\Exception\\HttpException",
"file": "D:\\AppServ\\www\\come\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php",

我搜索错误信息。结果是 csfr 问题。

但是我该如何解决这个错误呢? 我已经有了

$.ajax({
                headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },

这个问题不是第一次。这是第二次或第三次。

代码

$('.findNews_geography').autocomplete({   
        source: function(request, response) {
            var findtable=$('.findtable_num').val();
            var terms=request.term; 
            console.log("findtable="+findtable+";term="+terms);
            $.ajax({
                headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },


                url: "findNews_geography",
                dataType: "json",
                type: "post",
                data: {
                    findtable : findtable,
                    term : terms,
                },
                error: function(xhr, ajaxOptions, thrownError) {
                    console.log("findNews_geography ajax error="+xhr.responseText);
                    console.log("findNews_geography xhr.status="+xhr.status+";thrownError="+thrownError);
                },
                success: function(data) {
                    console.log("see request="+data);
                    response( $.map( data, function( item ) {
                        return {
                            label: item.place,
                        }

                    }));
                } //success end
            }); //ajax end
        }, //source end
        minLength: 0, 
}); //autocomplete end




 $(".findNews_geography").focus(function () {
         //if (this.value == "") {
       console.log("findNews_geography get focus");
        if($('.findtable_num').val()){
            $(this).autocomplete("search"); 
        }// }; 
  });

【问题讨论】:

  • 你的头部标签中有<meta name="csrf-token" content="{{ csrf_token() }}"> 吗?
  • 是的~我有
  • 在控制台中你看到请求发送了给定的令牌吗?
  • @ka_lin 在哪里可以找到?我将 chrome 控制台图像发布在『first was ok.第二个出错了』
  • 现在我想这是关于 laravel 的 ajax 延迟重播。但我不知道为什么以及如何解决它〜它发生不规则......

标签: laravel jquery-ui-autocomplete csrf-protection


【解决方案1】:
$('.findNews_geography').autocomplete({   
        source: function(request, response) {
            var findtable=$('.findtable_num').val();
            var terms=request.term; 
            console.log("findtable="+findtable+";term="+terms);
            $.ajax({
                headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },


                url: "findNews_geography",
                dataType: "json",
                type: "post",
                data: {
                    findtable : findtable,
                    term : terms,
                    _token: $('meta[name="csrf-token"]').attr('content')  
                },
                error: function(xhr, ajaxOptions, thrownError) {
                    console.log("findNews_geography ajax error="+xhr.responseText);
                    console.log("findNews_geography xhr.status="+xhr.status+";thrownError="+thrownError);
                },
                success: function(data) {
                    console.log("see request="+data);
                    response( $.map( data, function( item ) {
                        return {
                            label: item.place,
                        }

                    }));
                } //success end
            }); //ajax end
        }, //source end
        minLength: 0, 
}); //autocomplete end

尝试将 ajax request 中的 csrf 令牌作为数据发送

 data: {
          findtable : findtable,
          term : terms,
          _token: $('meta[name="csrf-token"]').attr('content')  
        },

希望对你有帮助

【讨论】:

  • 感谢您的建议!但是错误还在!
  • 非常感谢。我在一个名为easyAutocomplete的插件中使用了它并且奇迹般地工作,easyAutocomplete({ url : function(phrase) { return "/admin/api?t=myusers"; }, getValue : function(element) { return element.name; }, ajaxSettings : { headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
猜你喜欢
  • 2019-03-01
  • 2015-03-14
  • 2018-07-21
  • 2020-10-21
  • 2019-10-17
  • 2021-04-28
  • 2021-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多