Implementing a built-in JS script instead of an AJAX request. What for?

I noticed that some websites (e.g. apple.com or disqus.com) do not send an AJAX request for download, such as direct search results. Instead, they attach a new JS script that contains all the results in the variable.

To be more specific. If you go to apple.com and enter "test" in the search engine. It will not send an AJAX request to get the results, instead it will add <script src = "some.addres.apple.com/result.js?q=test"> in the HTML and execute a new JS script fragment to list the search results. improvement Do they only do this because you cannot send an AJAX request to another domain or is it possible to improve the speed?

+4
source share
4 answers

There are many discussions on the Internet about JSONP "hacking":

I found this extremely useful for overcoming the cross-domain security limitations, but I have not seen or heard of any speed improvements.

I highly recommend learning more about JSONP (especially when it's useful / necessary).

+1
source

This approach is called jsonp. Instead of calling an ajax request, the script adds a new script tag that contains the data included in the callback function (for example, onComplete callbacks for ajax calls). This is useful when you need a request to a different domain. Otherwise (in my opinion) ajax is preferable, for example. it has corresponding error , success and timeout callbacks.

more about jsonp

+1
source

One of the main advantages of JSONP technology is that since your search function is now served from another server, and you went ahead and took the pain to develop a JSONP response mechanism, you can continue and implement this search wherever you wish .

So, in your specific example, Apple can use the same search (if it encapsulates other ecosystems) to serve the results, for example, on another sister website.

In terms of performance, as pointed out by @fantactuka, AJAX calls are preferable, in my opinion, as well, since script injection is expensive and provides fewer ways to handle when stuff goes wrong.

+1
source

I believe that this is a tool for working with restrictions imposed by the browser, which should be directed to the same server from which the page was loaded.

0
source

All Articles