Bug Tracker

Opened 10 years ago

Closed 10 years ago

Last modified 9 years ago

#8211 closed bug (worksforme)

statusCode is incorrectly handled

Reported by: invisible_stefan Owned by: invisible_stefan
Priority: high Milestone: 1.next
Component: ajax Version: 1.5
Keywords: Cc: jaubourg
Blocked by: Blocking:

Description

Hi all,

I would like to react on http status-codes (like 200, 201 or 202) with the new feature (http://api.jquery.com/jQuery.ajax). But the function is ignoring my 201 and 202 callbacks.

This error occures with firefox-4.0_b10 and chromium-9.0

I'm looking forward to fix this little issue.

Regards Stefan

My code-snipped:

jQuery.ajax({
        url: url,
        dataType: 'json',
        statusCode: {
          404:function() { alert("404"); },
          200:function() { alert("200"); },
          201:function() { alert("201"); },
          202:function() { alert("202"); }
        },
        success: function(data){
          switch(data.status) {
            case 'done':
              /* display it to the User */
              break;
          }
        });

Change History (7)

comment:1 Changed 10 years ago by jitter

Cc: jaubourg added
Component: unfiledajax
Owner: set to invisible_stefan
Priority: undecidedhigh
Status: newpending

Thanks for taking the time to contribute to the jQuery project by writing a bug report.

Can you maybe submit a link to a reduced test case (some website which sends the appropriate headers), that reproduces the issue you are experiencing. So that we can investigate this issue further. Also make sure to read the link given below, in order to provide a most useful bug report.


How to report bugs

comment:2 Changed 10 years ago by anonymous

Hi all

I found a solution for the problem:

jQuery.ajax({
        url: url,
        dataType: 'json',
        statusCode: {
          404:function() { alert("404"); },
          200:function() { alert("200"); },
          201:function() { alert("201"); },
          202:function() { alert("202"); }
        },
      });

Somehow jQuery get's confused if the success/error feature is used along with the statusCode-Map.

Regards Stefan

comment:3 Changed 10 years ago by jaubourg

jQuery doesn't "get confused" at all:

http://jsfiddle.net/J2WxM/1/

Are you sure the code in your success handler doesn't throw an exception?

comment:4 Changed 10 years ago by jaubourg

Resolution: worksforme
Status: pendingclosed

comment:5 Changed 9 years ago by vasken@…

The provided example does work correctly, but consider the following:

http://jsfiddle.net/dmS2A/

This returns the appropriate 200 alert, but changing the url to a 404 page does not return the expected 404:

url: 'http://www.404errorpages.com/',
}}}a

comment:6 in reply to:  5 ; Changed 9 years ago by vasken@…

Sorry, the example for the second situation should read like this (essentially hitting against a 404 page instead of an existing page):

url: 'http://www.404errorpages.com/asdfasdfa.html',

comment:7 in reply to:  6 Changed 9 years ago by jaubourg

Replying to vasken@…:

Sorry, the example for the second situation should read like this (essentially hitting against a 404 page instead of an existing page):

url: 'http://www.404errorpages.com/asdfasdfa.html',

This example uses dataType jsonp and, as is stated on the documentation (see the error option description here: http://api.jquery.com/jQuery.ajax/), cross-domain jsonp requests won't notify errors... so no 404.

http://jsfiddle.net/54uXZ/ works perfectly.

Note: See TracTickets for help on using tickets.