CSRF令牌错误?在laravel Symfony \\ Component \\ HttpKernel \\ Exception \\ HttpException上 [英] CSRF token error? on laravel Symfony\\Component\\HttpKernel\\Exception\\HttpException

查看:642
本文介绍了CSRF令牌错误?在laravel Symfony \\ Component \\ HttpKernel \\ Exception \\ HttpException上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先还可以.第二个错误 我在laravel的javascript页面上使用了ajax函数

first was ok. second got error I use the ajax function on javascript page in laravel

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

If I initiate the function once it work well But when I start the function 2 or 3 times in short time I got the error

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

我搜索错误消息.结果就是csfr问题.

I search the error message . The result is the csfr issue.

但是我该如何解决该错误? 我已经有

But how can I fix the error? I have already have the

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

问题不是第一次出现.这是第二次还是第三次.

The question is not on the first time . It's on the second or third times.

代码

$('.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"); 
        }// }; 
  });

推荐答案

$('.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令牌作为数据发送

Try to send the csrf token in your ajax request as data

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

希望这会有所帮助

这篇关于CSRF令牌错误?在laravel Symfony \\ Component \\ HttpKernel \\ Exception \\ HttpException上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆