The AJAX is a "way" of using the Xmlhttprequest, which is not a language, but a API of Javascript, as well as File API, DOM API, etc..
What the XHR (Xmlhttprequest API) does is a client and server communication, does not mean that it is asynchronous, whereas AJAX is the way to use XHR "asynchronously".
Example of synchronous XHR (this is not AJAX):
var oReq = new XMLHttpRequest();
//Defina como false
oReq.open("GET", "/url", false);
//Espera completar a requisição, geralmente congela o browser
oReq.send(null);
alert(oReq.responseText);
Example of asynchronous XHR (this is AJAX):
var oReq = new XMLHttpRequest();
//Defina como true
oReq.open("GET", "/url", true);
//Função assíncrona que aguarda a resposta
oReq.onreadystatechange = function()
{
if (oReq.readyState === 4) {
alert(oReq.responseText);
}
};
//Envia a requisição, mas a resposta fica sendo aguardada em Background
oReq.send(null);
So Ajax means Tosynchronous JavaScript tond XML (translating is asynchronous Javascript and XML) and is the way to use XHR that makes it AJAX or not. The term AJAX would be a nickname for the XHR used asynchronously.
It is important to note that the synchronous mode was "nicknamed" SJAX, which means Synchronous JavaScript tond XML (Javascript and synchronous XML)), but also note that SJAX is out of use and modern browsers have begun to emit warnings on the console and will probably come to "block" this way, however within Web Workers, as it runs on a separate "thread" as well as the error message states:
synchronous xmlhttprequest on the main thread is deprecated
In other words, only in main thread, which refers to the main thread running on the tab, the Web Works perform on sub-threads, which causes no problem and in this case is not in disuse.
More details on https://xhr.spec.whatwg.org/#Sync-Warning
Another situation is that we don’t always use XML, but at the time the nickname came up it was quite used to have support for .resposeXML
XHR, to use JSON today we can do the parse
of .responseText
with something like:
var resposta = JSON.parse(oReq.responseText);
console.log(resposta);
Or you can adjust the property XMLHttpRequest.responseType
to "json" and use XMLHttpRequest.response
to get JSON, assuming the server response is actually JSON, leaving something like:
oReq.responseType = "json";
var resposta = oReq.response;
console.log(oReq.response);
Javascript/Ecmascript asynchronous and callback
To understand a little better about how callbacks run, read on:
The passage quoted is from a job vacancy?
– rray
@rray as always, the job vacancy ads always mitando.
– Wallace Maxters
What is the right one then? "It is necessary to know how to type in Ajax" or "It is necessary to know the libraries Ajax, jQuery..."...
– Diego Souza
@Zoom I would say that AJAX is a technique. To use this technique, you need to know the Xmlhttprequest API, Javascript and DOM, and also the concept of searching for information in the background, asynchronously, and updating only parts of the page without having to reload the entire page. There are also frameworks that abstract the way to use Ajax (using Java JSF, for example, you use Ajax by writing only Facelets tags, without having to write Javascript or interact directly with Xmlhttprequest and DOM).
– Caffé
@Caffe is right, it is absolutely right, it is a technique or way to use Xmlhttprequest!
– Guilherme Nascimento
I believe that the term AJAX is what is today called SPA (Single Page Application). I understand so when I see as a requirement "AJAX programming", IE, that it will be necessary to know how to program a system that uses a lot of JS, and not just server-side language with constant refreshes.
– lucasDotCom
@lucasDotWith I think you’re confusing, ajax doesn’t need to be used to load a page or refresh a page, it’s used for functions like fetching notifications, messaging chats and sending data, but that doesn’t mean the whole site is going to be based on Ajax, often we use it only on a specific feature. What we can say is that the SPA makes use of Ajax, but not the other way around.
– Guilherme Nascimento
William. I didn’t really mean it. When I see "AJAX Programming" as a requirement, I imagine it’s not just to make a request to search for a notification, to search for a message, a Long Polling or a request triggered by an event. Yes, this is AJAX, but I referred to it as a vacancy requirement and not as a concept. A SPA is much more complex than just a simple request. Anyway, it seems a little confusing, because I ended up not expressing myself right.
– lucasDotCom
It’s the new PHP version hahahahaha
– bfavaretto