Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / JSONP

jQuery 1.5 broke my JSONP request, and its my own damn fault.

5.00/5 (4 votes)
5 Apr 2011CPOL2 min read 22.6K  
jQuery has changed its implementation of how it creates JSONP requests in a way that can break some applications.
The Problem
Yesterday here at CodeProject Central, we tried to update to the latest and greatest version of jQuery. When we did, a very important JSONP request started to fail. There was a JavaScript parse error and the JSONP callback was not executed.

What I Saw
Using IE9 as well as Firebug, I looked at the request and response. What I found was that the remote server was responding with valid JSONP, but the browser was complaining that the wrapping JSONP callback function did not exist. Further examination showed that the returned JSONP wrapper method did not match the requested callback paramater.

Since we use caching a lot on the remote server I suspected a caching problem.

What I did Wrong
On the remote server, I was caching the entire response. I had observed that in jQuery 1.4.X, the callback function name in the request had the format of JSONPNNNNNN... So, prior to returning the cached response, I used some Regex magic to replace the cached method name with the name in the current request.

jQuery 1.5.1 has changed how it generates the callback function name by default. Now it has the format similar to jQuery15108338082315804084_1298857654378 which broke my magic Regex, so I was alway returning the cached data, including the old JSONP callback.

How I Fixed it
What I did to fix it required 2 changes:
1. Instead of caching the whole response, I only cache the JSON portion.
2. moved the code to wrap the JSON data in the JSONP callback to just before returning the response, after any cache calls.

What I learned, again
Don't build code on specific implementation details that are not fixed in stone. Here I assumed that the way jQuery generated callback names was fixed. It wasn't.
I fact, if I had specified the callback name, instead of passing "?", it would have failed.
Also, only cache what you need to.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)