简体   繁体   中英

HTTP Status 403 - Bad or missing CSRF value but the csrf token is set

I have an ajax request which looks like this:

$.ajax({
   headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
       },
       'url': defaults.addToCartUrl,
       'data': JSON.stringify({CSRFToken: Config.CSRFToken,currentUser: currentCustomer, entries: cartItems}),
       'type': 'POST',
       'dataType': 'json',
       beforeSend: function(xhr) {
           xhr.setRequestHeader('X-CSRFToken', Config.CSRFToken);
        },
        'success': function (data, textStatus, jqXHR) {

        },
        'error': function (jqXHR, exception, m) {
             console.log('Cannot move products from 
         }
     });

The problem is that I keep getting this HTTP Status 403 - Bad or missing CSRF value but I set the token as a parameter in the data payload as well as on the request header.

在此处输入图片说明

Isn't the beforeSend supposed to set the token to "X-CSRF-Token"? Maybe use ajaxSetup for your headers?

Example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM