Home Tech Geek Web Development Solution to: How do I use jQuery on cross-site domains using jsonp?

Get social!

I needed to access mapping data from mapquest.com’s API. Using the standard jQuery $.ajax() function used along with jsonp seemed my best bet, since the jsonp data type allows for cross-domain ajax calls. The code looked something like this:

Now, I admit I was a bit rusty using the jsonp callbacks with jQuery. I expected either the success function to fire or the renderDirections function to fire. Well, the error function fired instead. But that’s not the weird part. The weird part is that the passed result object was the result object you’d expect to receive in the success function. OK — 200 and all that (including the response text). What the what!?

Solution

So I hit the docs and found that jQuery appends the "callback=" to the url automatically. I simply removed the reference to renderDirections in the url and the problem disappeared (the success function fired). The working code looked like this:

Leave a Reply

Bad Behavior has blocked 39 access attempts in the last 7 days.