Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AJAX Basics (retiring) jQuery and AJAX Handling Errors

Noel Deles
Noel Deles
8,215 Points

$.getJSON error handling with JSONP??

I've done some research and am aware of chaining the .fail() method. However, if working with JSONP this no longer works. Dave mentions in the video that you must "check the response in the normal success callback to see how the server responded". What exactly does this mean? How would I go about handling errors in the flickr challenge for example?

1 Answer

rydavim
rydavim
18,814 Points

In the jQuery documentation for ajax requests, they note the following:

error A function to be called if the request fails. ... This handler is not called for cross-domain script and cross-domain JSONP requests.

The workaround seems to be setting a timeout to execute an error callback. If the request hasn't responded successfully, assume it's failed and write an error handler. It's certainly not a pretty solution, but it seems to be the accepted one.

Your other option is to use this jquery-jsonp plugin from jaubourg on github. His plugin features error recovery.

Happy coding! :)

Noel Deles
Noel Deles
8,215 Points

Yep, after further reading seems like that is the accepted solution if I don't want to use a plugin. For those looking for a code example, you can use this as a reference: click