I think google.com uses AJAX, but when I looked at the source of google.com, I didn't find "XMLHttpRequest".
Also I think phpMyAdmin uses AJAX, but when I looked at the source of phpMyAdmin, I didn't find "XMLHttpRequest".
So how are google.com and phpMyAdmin using AJAX without using XMLHttpRequest
? could they be using a library and this library uses XMLHttpRequest
?
CodePudding user response:
Ajax is a general term meaning "Make an HTTP request from JavaScript without leaving the page". There are numerous ways to do that and some of them are more debatable than others.
- XMLHttpRequest is the classic approach.
- Fetch is the modern approach and has some benefits (such as a Promised based API) but lacks some features (notiably upload progress monitoring) that XHR has.
- JSONP works by injecting a
<script>
element into the page which calls a callback when it loads. It's a hack to get around the Same Origin Policy that has some security risks and has been superceeded by CORS. - Injecting any kind of element that accesses a remote URL (such as an image) lets you make an HTTP request (but not read the response).
- It looks like you can handle HTTP requests in Web Assembly too.
There are numerous libraries which wrap themselves around those various APIs.
CodePudding user response:
To answer your question about whether google.com uses XMLHttpRequest, the answer is yes.
Using Chrome, open the dev tools and go to the network tab. Then, in the search box of the webpage, type a letter as if you were going to do a web search. Google then sends an Ajax XMLHttpRequest to a server to get autocomplete suggestions based on the letter you typed.
I typed the letter "a", which resulted in several requests, one of which was to a url beginning with "search?q=a". This can be seen in the "Name" column in the network tab. To the right is a column "Type", which shows the request is an XHR request (meaning it was made via an XMLHttpRequest - fetch requests will have "fetch" as the value here). Further right is the column "Initiator", which references the Javascript that initiated that network request.