0

I am using ajax replay and during ajax call I want to update the X-CSRF-TOKEN value but I am not able to change the X-CSRF-TOKEN sending old value.

$.ajax({
         url: url,
         type: requestType,
         contentType: contentType,
         data: data,
         beforeSend: function(jqXHR, settings) {
             if (typeof beforeSendCallbackFunction === "function") {                
                 beforeSendCallbackFunction();
             }
         },
         success: function(response, textStatus, jqXHR) {
            if(typeof JSON.parse(response) =='object'){
                var value = JSON.parse(response);
                if('_token' in value) {
                if(value._token != 'logout') {                    
                    $.ajaxSetup({
                        headers: {
                            "X-CSRF-TOKEN": value._token
                        },
                        async: false
                     });
                   $.ajax(this);
                   }
               }
            }
        }
    });
Shahzad Intersoft
  • 762
  • 2
  • 14
  • 35
  • Arn't you doing the ajaxSetup piece in the `success` function?? That runs once the response has been received from the server portion of this conversation i.e. well after when you should be doing this which is before the ajax call is made – RiggsFolly Feb 07 '20 at 13:03
  • @RiggsFolly sorry i am not getting your point – Shahzad Intersoft Feb 07 '20 at 13:05
  • So `$.ajax(this)` is supposed to repeat the same request again, after you changed the ajax setup defaults? (Does that actually _work_? Never seen that used before, and not sure what `this` actually points to here either.) You have taken measures then to ensure that your endpoint does not again return an object that contains a `_token` property, so as to not create an endless loop here …? – 04FS Feb 07 '20 at 13:13

0 Answers0