jQuery add CSRF token to all $.post() requests' data

前端 未结 7 1568
后悔当初
后悔当初 2020-11-30 03:02

I am working on a Laravel 5 app that has CSRF protection enabled by default for all POST requests. I like this added security so I am trying to work with it.

While m

相关标签:
7条回答
  • 2020-11-30 04:02

    I think that above solution might not work as well. When you do:

    var x; 
    x + ""
    // "undefined" + empty string coerces value to string 
    

    You will get data like "undefined_token=xxx"

    When you use the above solution for laravel's delete for instance you have to check like this:

    if (typeof options.data === "undefined")
        options.data = "";
    else
        options.data += "&";
    options.data = "_token=" + csrf_token;
    
    0 讨论(0)
提交回复
热议问题