How to access the API using jQuery
jQuery has cool methods like getJSON, get and load. However, they all end up calling AJAX.
I am trying to access the www.eventsinindia.com/cities/mumbai/events.js?month=2009-05 API.
This API call returns data in JSON format.
I couldn't find a way to call this API from jQuery and get the output in JSON format. I keep accessing the restricted URI, rejected the "code:" 1012 error because jQuery is trying to make an AJAX call. AJAX call from a separate page to the server is prohibited.
How does it work on a different domain, are you using a JSONP callback?
http://docs.jquery.com/Ajax/jQuery.getJSON
As of jQuery 1.2, you can load JSON data located on a different domain if you provide a JSONP callback, which can be done like this: "myurl? Callback =?". jQuery automatically replaces? with the correct method name to call by invoking the specified callback. This callback parameter can vary by API, for example Yahoo Pipes requires "_callback =?"
As @ceejayoz JSONP suggested , the method should be used to access data in a different domain. But for this to work, the server-side script must be JSONP enabled, which means it must take a parameter that will specify the name of the client's callback function to append to the JSON data. If it is not, you need to write a server script on the domain that hosts your client script in order to bridge the external domain.
If it's not a cross-domain request, you just need to:
jQuery.getJSON("/cities/mumbai/events.js?month=2009-05", function(json) {
alert(json[0]);
});