I got inspired by this awesome article by Gabriel Weinberg to hack a bit on nginx.
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:
|
|
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: callback=jQuery123123
.
To achieve the same with nginx, I modified the snippet:
|
|
Now if we take a simple JSON file:
|
|
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:
|
|
Enjoy