$.ajax using 'context' settings sets Content-Type for GET requests
See original GitHub issueExample:
$.ajaxSetup({
statusCode: {
401: function () {
var $settings = this;
var auth = $('link[rel~=authenticate]').prop('href');
var opts = { xhrFields: { withCredentials: true } };
$.ajax(auth, opts)
.done(function () {
$.ajax($settings);
})
.fail(function () {
showErrorModalOrSomething();
});
}
}
});
// This causes the statusCode handler above to trigger, which in turn
// sends the re-authentication request (which is successful) and then
// retries this same request. But when it retries this same request,
// the Content-Type header for the GET request is set to
// 'application-x-www-form-urlencoded' (due to the $settings object above),
// which has undesirable side effects with certain server-side frameworks
$.get(urlThatResultsIn401AtFirst, function (partial) {
getModal().find('.modal-dialog').html(partial);
});
I would like to use this same handling across both GET and POST requests. I am open to workarounds if anyone has an idea.
Issue Analytics
- State:
- Created 8 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
What is content-type and datatype in an AJAX request?
contentType is the type of data you're sending, so application/json; charset=utf-8 is a common one, as is application/x-www-form-urlencoded; ...
Read more >jQuery.ajax() | jQuery API Documentation
A set of key/value pairs that configure the Ajax request. All settings are optional. A default can be set for any option with...
Read more >jQuery ajax contenttype | Learn the Working of ajax ... - eduCBA
The ajax() function is used to perform an asynchronous HTTP request to the server and by using the contenttype option it describes to...
Read more >jQuery Ajax Function: How to Make Asynchronous HTTP ...
Learn how to use $.ajax(), the most powerful jQuery Ajax function, to perform asynchronous HTTP requests.
Read more >jQuery and Ajax Tutorial
AJAX Interface: jQuery provides a simple Ajax interface to send asynchronous HTTP GET/POST requests and process the response. With jQuery, you can write...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@dmethvin: that is fine if it is supposed to be the default value, but please consider:
Content-Type
makes virtually no sense on aGET
requestLooks correct to me, the
contentType
is set to its default since it wasn’t specified in the original request, as mentioned in thejQuery.ajax
API docs. If the server wants a different value you should set it in the options.