Understanding Cross-Domain XHR and XML Data

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.

0
source share
2 answers

@adeneo answered a question, but in a comment. Therefore, my understanding of JSONP was fundamentally wrong. When a JSONP request is made, it is not an XHR request. Rather, you should specify a dynamic script tag and get JSON . Thus, although the call looks like XHR (at least IMO jQuery), it is not. The XMLHttpRequest object is not used at all.

This question has already been answered. What is JSONP? but I somehow missed it before. Another good resource explaining the Cross Domain request is on devlog

The remaining problems that I raised become redundant!

0
source

Q) Is it possible to use XML data instead of JSON in a similar way?

no, because JSONP is not json, it is javascript. The client requires a script from a server that runs on the client. "JSONP" is a trick that uses a script tag to get a javascript object. You could send the XML to a string, albeit in a javascript object.

Q) The only way I can think of this is to proxy data through the same domain. Is this understanding correct?

or make CORS server support

http://en.wikipedia.org/wiki/Cross-origin_resource_sharing

My point is that if the domain does not allow default X-origin requests coming from client scripts, you cannot do anything about it. Some browsers may allow this, but this is not the default behavior. In this case, the only option is a proxy server in the same domain.

+1
source

Source: https://habr.com/ru/post/1479019/


All Articles