AJAX VS JSon "Cross-Domain Embedded Security" Question

It seems like I cannot call outside the current domain name using "AJAX". But I can call the twitter API (with JSON) in JQuery ... do not both use an XMLHTTP object? If yes (or no), why can I name another domain name with JSON (using JQuery), but not with AJAX? What is the difference between Ajax and JSON anyway?

+4
source share
2 answers

The twitter API uses JSONP, which does not use XMLHTTPRequest, but uses the <script> to enable foreign javascript. This script then calls the function in your own javascript.

Integration with Google Maps on third-party sites is impossible without this โ€œhackingโ€ (this is actually what it is: hacking).

More on JSONP:

http://ajaxian.com/archives/jsonp-json-with-padding

or in the wikipedia article on JSON:

http://en.wikipedia.org/wiki/JSON#JSONP

+7
source

JSON is a file format, while AJAX is a JavaScript method for sending and receiving data from a web server after a page has finished loading. X is in AJAX for XML (also a file format), which is an alternative to JSON, but it is a little wrong because quite a few things that people call AJAX do not actually use XML at all.

Mr. LeyBaert's JSONP link is very useful in terms of practically explaining what is going on in the Twitter API; this is not XMLHTMLRequesting at all, there is just another script tag that points to the javascript file on the Twitter website, which then loads along with the rest of your page when the page first loads.

You can get neat things through this, but I donโ€™t think you can do it after loading the page (as with AJAX), unless you start messing with hidden IFrames or the like. If you really need to get AJAX style elements without XMLHTTPRequests, what do you want to learn; dynamically adding an IFrame to a document that links to a page that requests a script (or the like) from another site. There is a discussion of the pros and cons of this here .

0
source

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


All Articles