Adding CSRFToken to Ajax request

后端 未结 9 2125
夕颜
夕颜 2020-11-27 05:40

I need to pass CSRFToken with Ajax based post request but not sure how this can done in a best way. Using a platform which internally checking CSRFToken

9条回答
  •  清歌不尽
    2020-11-27 06:12

    You can use this:

    var token = "SOME_TOKEN";
    
    $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
      jqXHR.setRequestHeader('X-CSRF-Token', token);
    });
    

    From documentation:

    jQuery.ajaxPrefilter( [dataTypes ], handler(options, originalOptions, jqXHR) )

    Description: Handle custom Ajax options or modify existing options before each request is sent and before they are processed by $.ajax().

    Read

提交回复
热议问题