Reputation: 110
Is there any way to know what is the method (GET/POST/PUT/DELETE) of an ajax request in Javascript or Jquery?
I went through the docs but I couldn't get a solution. My aim is to set headers if the ajax request is not of GET method.
Upvotes: 0
Views: 156
Reputation: 110
I was able to solve this.
$(document).ajaxSend(function(e, xhr, options) {
if(options.type != "GET") {
xhr.setRequestHeader(HEADER, VALUE);
}
});
options gives the type of the request that's being made.
Thank you everyone for help!
Upvotes: 0
Reputation: 1726
Instead of using jQuery for making AJAX calls, recommend using either native fetch
or in case you need to support older browsers you can use https://github.com/github/fetch.
By default AJAX calls will be GET
calls. If you want to use another HTTP method, then you need to set the method
as an option. For example using native fetch
fetch('someURL', {
credentials: 'same-origin',
method: 'POST',
body: JSON.stringify(payload),
});
Another good read is https://davidwalsh.name/fetch.
This is while making the ajax call. If you want to what was the original call made from the response
you get, unless the server explicitly sets the value in the header, I don't think you will be able to figure that out.
Upvotes: 1