CSRF token error? on laravel Symfony\\Component\\HttpKernel\\Exception\\HttpException

我怕爱的太早我们不能终老 提交于 2019-12-11 14:51:30

问题


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

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",

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.

Code

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

回答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

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

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

Hope this helps



来源:https://stackoverflow.com/questions/49850695/csrf-token-error-on-laravel-symfony-component-httpkernel-exception-httpexce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!