For example, I have:
$.ajax({
type: 'POST',
url: 'https://jsonplaceholder.typicode.com/todos',
data: { name: 'random name' },
headers: { 'x-my-custom-header': 'XYZ' },
success: function(successResp) {
console.log(successResp);
},
error: function(errorResp){
console.log(errorResp);
}
});
How can I access ‘x-my-custom-header’ from the request header within success or error callbacks?
3
Answers
If you don’t explicitly include a
context
property in the$.ajax()
settings, then in your “success” etc. handlersthis
will refer to the settings object itself. Thus,this.headers
will give you the additional header properties.More information at MDN.
Store your header in a variable.
You can try this, as
request
object is accessible if requesting from success callback as third parameter:});
And for more details, please visit : https://api.jquery.com/jQuery.ajax/