I have been working with JavaScript and AJAX for quite some time, I would like to understand how Cross Domain XHR really works and how JQuery handles it, for some reason I never worried about how it works. I read the article Wikipedia JSONP article , and I'm more confused. I am not sure what I do not understand.
I know that using JSONP , I can use JSON data directly in JavaScript. For example, this example is JS Fiddle . Here I use JSON to display a list of images. Can I achieve the same using XML data instead? Please read the rest of the analogy before answering this part of the question.
1) If I try something like below or a Fiddle link I get an Uncaught ReferenceError: jsonFlickrFeed is not defined error message Uncaught ReferenceError: jsonFlickrFeed is not defined
$.ajax({ url: "http://api.flickr.com/services/feeds/photos_public.gne", data: { format: "json" }, dataType: "jsonp", success: function(d) { console.log(d); } });
2) The example below or the link for the script works fine
$.ajax({ url : "http://api.flickr.com/services/feeds/photos_public.gne", data: {format: "json"}, dataType: "jsonp" }); jsonFlickrFeed = function(d){ console.log(d); }
Q) I believe that between 1 and 2, since the returned data is in a format like jsonFlickrFeed({}) , do we need to write a jsonFlickrFeed callback function to make it work?
Q) Why does it never call a success callback?
Q) Is this the Flickr endpoint that does the job of returning JSONP (by which I mean jsonFlickrFeed({}) data)? Or does it just return the actual JSON and jQuery?
3) With $.getJSON code looks like below or fiddle
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?", { format: "json" }, function(d) { console.log(d) });
Q) How will jQuery take care of this in case 3)? I see that the returned data is in the format jQuery1820349100150866434_1355379638775({}) So, if I assume that JQuery is doing the job of associating JSON with a callback, is this correct?
Q) For this reason, is it called an abbreviated jQuery method?
From what I tried, I was not able to use the XML data. I could not figure out how to use XML data instead of JSON.
Q) Is it possible to use XML data instead of JSON in a similar way?
Q) The only way I can think of this is to proxy data through the same domain. Is this understanding correct?
If that helps, Sample XML I have a dropbox. To demonstrate that XML data can be parsed when it comes from the same domain.