question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

$.ajax using 'context' settings sets Content-Type for GET requests

See original GitHub issue

Example:

$.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:closed
  • Created 8 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tuespetrecommented, Mar 1, 2016

@dmethvin: that is fine if it is supposed to be the default value, but please consider:

  • Content-Type makes virtually no sense on a GET request
  • The current implementation is inconsistent. Why would the default header not be set for the first request, but be set on the options the second time around?
0reactions
dmethvincommented, Mar 1, 2016

Looks correct to me, the contentType is set to its default since it wasn’t specified in the original request, as mentioned in the jQuery.ajax API docs. If the server wants a different value you should set it in the options.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found