Remove specific request headers set in jQuery.ajaxSetup
13,099
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
Related videos on Youtube
Author by
Salman
Updated on June 14, 2022Comments
-
Salman about 2 years
I setup some custom headers using
$.ajaxSetup({ headers : { 'x-custom' : 'value' } });
It will add
x-custom
header for all the ajax request. But I want some specific requests to NOT contain this header.I tried this, delete header from ajaxSettings before that ajax call and add it back when its completed
delete $.ajaxSettings.headers["x-custom"]; $.ajax({ ... "success": function (data) { $.ajaxSettings.headers["x-custom"] = 'value'; ... } });
But I feel this is not the correct way, as the request that fired before finishing that call will not get that header. What else can I do please suggest.
Should I add the header back in the next line after
$.ajax
instead doing it in callback?-
Arun P Johny about 10 yearsa more correct approach could be
delete $.ajaxSettings.headers["x-custom"]; $.ajax(...); $.ajaxSettings.headers["x-custom"] = 'value';
-
Salman about 10 yearsYes, I have done it that way. Just wanted to find out if there are any other/better way of doing it. Thanks.
-
Jason Kim almost 10 years
delete $.ajaxSettings.headers["x-custom"];
did the trick for me. I am using it now.
-
-
Salman almost 10 yearsThis would be wrong, It's same as adding those headers back in the callback, which isn't correct. I only wanted one specific request to not have those headers. So it seems like adding it back immediately is the best bet.