drg5577 2018-01-15 10:02
浏览 81
已采纳

经过多次尝试,Laravel 5.5在ajax调用上持续获得419个错误

I would really appreciate some help on this. I tried every solution that every one suggested on other posts, including this: Laravel 5.5 ajax call 419 (unknown status) and this Ajax LARAVEL 419 POST error solutions.

But I keep getting 419 error even if I set the csrf_token meta tag on the head section:

<meta name="csrf-token" content="{{ csrf_token() }}">

And before ajax post call, I set up the ajax csrf_token like this:

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

And after that I send the csrf_token in ajax data as a variable like this:

$.ajax({
        url     : "/getCompare",
        type    :"POST",
        cash    : false,
        data    :{ id : id,_token: '{{csrf_token()}}'},
        success :
           function( response ) {
              $("#compare_products").html(response);
             }
        });

The application of the page is in such way that the user repeatedly requests for ajax calls to compare products. It works fine in lots of attempts but in a specific scenario, it throws 419 error. The scenario is sometimes when I push a button about 3 minutes after page load, it gives me that error. Or sometimes it gives the error on the third or fourth attempt. And after reloading the page the problem fixed but it keeps sending that error again in the way that I explained.

My question is why is this happening? is csrf_token expired after 3 minutes or what? And I don't want to add an exception in verifyCsrfToken middleware. Whats the solution to this problem?

PS: leorent's answer is quite right. But after uploading my project into an actual online server. the problem was gone already. I don't know why but this problem is because of windows and running the laravel project locally in local host. The csrf token session expires frequently on widows localhost server. Don't worry, after uploading the project into an actual server, your problem with csrf is going to be solved automatically.

  • 写回答

1条回答 默认 最新

  • douchen2011 2018-01-15 10:14
    关注

    Add the meta tag with an id:

    <input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">
    

    then on your ajax call:

         $.ajax({
             url     : "/getCompare",
             type    :"POST",
             cash    : false,
             data: {'id': id, "_token": $('#token').val()},
             type: 'POST',
    
    
             success: function (response) {
    
             },
             error: function (response) {
    
             }
         });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵