Remove specific request headers set in jQuery.ajaxSetup Remove specific request headers set in jQuery.ajaxSetup ajax ajax

Remove specific request headers set in jQuery.ajaxSetup


Since this question doesn't have any answer that can be marked as Accepted. I am posting the solution.

Looks like adding back the header immediately after the AJAX call would make sense. This way we won't be waiting for success callback and then adding it.

delete $.ajaxSettings.headers["x-custom"]; // Remove header before call$.ajax({    ...    "success": function (data) {        ...    }});$.ajaxSettings.headers["x-custom"] = 'value'; // Add it back immediately


You could add an ajaxComplete function. It will run after all your ajax requests and do whatever you wish.
Something like this,

$(document).ajaxComplete(function(event, xhr, settings) {        // Add the headers again.        $.ajaxSetup({            headers : {                "x-custom" : "value"            }        });    }});  

You can find the documentation here.
Also, as of jQuery 1.8, the .ajaxComplete() method should only be attached to document.