The implementation of JSONP in jQuery kinda sucks. Part of the point of jQuery is to abstract away the underlying differences between implementations (mainly browsers) and provide a common interface. You would think this is true across the board but its not quite – well at least when it comes to JSONP.
Support for JSONP is found within the Ajax part of the framework. When using this API, you make basic assumptions about the contact it presents – i.e. that is supports contrcuts like promises for all requests, or being able to have success/error/complete methods run if you provide them. It turns out that because of the abstraction that jQuery provides, this is true for all Ajax requests, except JSONP.
The guys who worked on this code obviously intended that people should be able to treat JSONP requests just like any other AJAX request – providing a nice level of abstraction and providing a common interface. But when it comes to JSONP, about the only contruct it does support is “success” – meaning if there is a 404 with your request or anything else goes wrong, “error” will never run, likewise, “complete” never runs as well. Due to the common API and the fact that these are fairly basic constructs which one could mistake quite easily should run for each type of request. It turns out this is the assumption that I made which had me scratching my head for quite some time.
In the defence of the authors, the documentation (http://api.jquery.com/jQuery.ajax/) does state that these constructs/handlers aren’t supported with JSONP, by my argument is that if so much isn’t supported, then it shouldn’t be a part of this interface. But I digress, I didn’t start this just to complain.
Given the fact that I don’t think its acceptable to not know whether your query/request has succeeded or not, I set out to find an alternative. In my search I found Julian Aubourg’s (@jaubourg) solution where he has provided the support for JSONP that should have been included out of the box (i.e. error checking)! – https://github.com/jaubourg/jquery-jsonp
See it, love it and use it. I don’t know why the guys in jquery have decided that JSONP should have better support out of the box, but at the current time there is no better alternative.