It’s a great article which I highly recommend to read.
The part that specifically interested me was turning JSON into JSONP compatible calls, usable both for proxying remote / local apis that doesn’t supply JSONP functionality and for simple to padd local static JSON files.
What bothered me though was the fact that the name of the method to pad into the JSON response was hard coded.
Here’s Gabriels code snippet:
1 2 3 4 5
When you use jQuery to do an ajax request, you add
?callback=? to your url, which will be replaced by a random function name, E.g:
To achieve the same with nginx, I modified the snippet:
1 2 3 4 5 6 7 8
Now if we take a simple JSON file:
1 2 3 4
And call nginx to fetch it via the location we configured in the snippet above, with the url
http://server.com/json/sample.json?callback=Ahoy we’d get:
1 2 3 4