Bug Tracker

Ticket #11680 (closed bug: patchwelcome)

Opened 3 years ago

Last modified 21 months ago

Cross-Domain ajax request error handler does not get called

Reported by: geoffreyk Owned by:
Priority: low Milestone: None
Component: ajax Version: 1.7.2
Keywords: Cc: jaubourg
Blocking: Blocked by:

Description

Revisiting, and expanding http://bugs.jquery.com/ticket/11575

Using this code with 1.7.2 and edge

$.ajax({

type: 'get', url: ' http://openapi.ro/api/companies/123456789.json', dataType: 'jsonp', success: function(data) {

alert('success 404');

}, error: function(data) {

alert('fail 404')

}

});

$.ajax({

type: 'get', url: ' http://widgets.membership.s-msft.com/v1/user/usercard/?id=badID', dataType: 'jsonp', success: function(data) {

alert('success 500');

}, error: function(data) {

alert('fail 500')

}

});

jsfiddle link:  http://jsfiddle.net/geoffreyk/GTNX8/

The first ajax call is to a page that will return a 404 error code. The second ajax call is to a page that will return a 500 error code.

I am seeing in the following browsers, all on win7: FF12:

404 and 500 error functions are NOT called

Chrome 18:

500 error function is NOT called
404 error funciton is called

Chrome 18:

500 error function is NOT called
404 error funciton is called

IE8:

404 and 500 error functions are called

IE9:

404 and 500 error functions are called

Opera 11:

404 and 500 error functions are NOT called

bummer :(

Change History

comment:1 Changed 3 years ago by geoffreyk

arr, dropped a br. should be a break between "all on win7: " and "FF12:" Just to clarify, All of these tests were on windows 7. The first test was on FF12.

comment:2 Changed 3 years ago by geoffreyk

One more test IE7 on Vista: 404 and 500 error functions are called

comment:3 Changed 3 years ago by dmethvin

  • Cc jaubourg added

This is basically a dup of #10256, but at minimum the docs need some clarification about when the error callback may occur.

For example, it looks to me that the script transport is being used in Chrome and the script tag's .onload() fires even though the script URL is 404. Since there was no actual script content the jsonp callback didn't get called, so the error callback fires since there is no json to parse.

@jaubourg can you weigh in on this? Seems like we can't say the error callback will *not* be called, but only that it isn't called reliably on all browsers.

comment:4 Changed 3 years ago by jaubourg

We can be more precise in the docs but last time I checked onload didn't trigger for chrome in case of a 404. It's more likely that onerror doesn't trigger for a 500.

If people need reliable error handling for jsonp, they'd better use jQuery-JSONP (  https://github.com/jaubourg/jquery-jsonp ) and, if there are bugs there, to open issues against it.

I hope to be able to bring jQuery's jsonp implementation up to speed with the plugin but:

  1. it has a cost in size
  2. until Opera ticks to a new version, there is sniffing involved

comment:5 Changed 3 years ago by geoffreyk

IMHO, this is exactly what jQuery has always been and should continue to be about. Making difficult things simple and unifying the differences between browsers.

JSONP is not simple to implement, but the interface that jQuery has created makes using it so simple, that there are no reasons not to use it.

Except that to have really robust AJAX, you have to have good error handling. Stuff just breaks on internet, and those errors must be handled.

All smiles and rainbows, except once again, the browsers are handling errors differently. This is where jQuery has always shined. Bringing all the different browser implementations together so that we lowly devs can go out and create great code without worrying about the incompatibilities.

If jQuery is going to provide JSONP, then I believe it also needs to handle errors states correctly too. This should not be handed off to a plug in.

My $0.02

comment:6 follow-up: ↓ 7 Changed 3 years ago by dmethvin

@geoffreyk, looking forward to your patch!

comment:7 in reply to: ↑ 6 Changed 3 years ago by geoffreyk

Replying to dmethvin:

@geoffreyk, looking forward to your patch!

I am only qualified to use it, cheerlead for it, but not create it :)

I will give it a look though, but I think it may be over my head...

Version 0, edited 3 years ago by geoffreyk (next)

comment:8 Changed 3 years ago by dmethvin

This is an open source project, if you want something to happen you can make it happen. We're not opposed to this change at all, just not convinced it can be done given the state of browser support.

Honestly I think you are halfway there. For the browsers that don't yet support error events on script elements, create a non-jQuery test case and file a bug report with them, or find an existing one and vote it up. Then put the links here so they can be easily tracked. The good news is that the older browsers like IE8 do what you want already.

comment:9 Changed 3 years ago by dmethvin

  • Priority changed from undecided to low
  • Resolution set to patchwelcome
  • Status changed from new to closed
  • Component changed from unfiled to ajax

To reiterate, we're open to standardizing behavior here but don't think the browser support is able to do it. If someone notices in the future that browsers have cleaned up their act, or thinks up another way to do this, please let us know.

comment:10 follow-up: ↓ 11 Changed 21 months ago by guitarsolo92@…

$.ajax({

url:'http://maps.googleapis.com/maps/api/geocode/json', data:{'sensor':'true', 'address':search}, type:'GET',  cache:true, dataType:'jsonp', beforeSend: function (jqXHR, settings) {

alert(settings.url); Show-me if is a valid URL, for tests

}, success: function(r) {

alert((r).toSource());

}, error: function (jqXHR, textStatus, errorThrown) {

alert(textStatus + '\n' + errorThrown);

}

});

Alert Response#1  http://maps.googleapis.com/maps/api/geocode/json?callback=jQuery19107215280201753225_1361453726673&sensor=true&address=rua+araguari+118+bh+mg

Alert Response#2 parsererror Error: jQuery19107215280201753225_1361453726673 was not called

Whyyy?? i search all the web for that soluction and nobody know!!

comment:11 in reply to: ↑ 10 Changed 21 months ago by jaubourg

Replying to guitarsolo92@…:

Whyyy?? i search all the web for that soluction and nobody know!!

You need to brush up on those searching skills:  http://stackoverflow.com/questions/5359224/parsererror-after-jquery-ajax-request-with-jsonp-content-type

Note: See TracTickets for help on using tickets.