Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between AJAX and XMLHttpRequest [duplicate]

What is the difference between XMLHttpRequest and AJAX? Can anybody provide some examples to know the difference in terms of functionality and performance?

like image 465
Neha Avatar asked Dec 30 '17 04:12

Neha


People also ask

What is the difference between XMLHttpRequest and AJAX?

Ajax allows us to send and receive data from the webserver asynchronously without interfering with the current state or behavior of the web page or application. XHR is the XMLHttpRequest Object which interacts with the server.

What is the purpose of XMLHttpRequest in AJAX?

XMLHttpRequest (XHR) objects are used to interact with servers. You can retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing. XMLHttpRequest is used heavily in AJAX programming.

What is the difference between AJAX and fetch?

Fetch is a browser API for loading texts, images, structured data, asynchronously to update an HTML page. It's a bit like the definition of Ajax! But fetch is built on the Promise object which greatly simplifies the code, especially if used in conjunction with async/await.

What is difference between fetch and XHR?

The Fetch API allows you to make network requests similar to XMLHttpRequest (XHR). The main difference is that the Fetch API uses Promises, which enables a simpler and cleaner API, avoiding callback hell and having to remember the complex API of XMLHttpRequest.


2 Answers

XMLHttpRequest can be used to retrieve any type of data, not just XML, and it supports protocols other than HTTP including file and ftp.

var XMLHttpRequest = new XMLHttpRequest();
XMLHttpRequest.onreadystatechange = function() {
    if (XMLHttpRequest.readyState == XMLHttpRequest.DONE) {
        console.log(XMLHttpRequest.responseText);
    }
}
XMLHttpRequest.open('GET', 'http://google.com', true);
XMLHttpRequest.send(null);

AJAX stands for Asynchronous JavaScript And XML. it is the use of the XMLHttpRequest to communicate with servers.

It can send and receive information in various formats, including JSON, XML, HTML, and text files.

var request = $.ajax({
    type: 'GET',
    url: "http://google.com",
    async: true,
    success: function() {
        console.log('sucess');
    }
});
like image 95
Parth Raval Avatar answered Sep 28 '22 21:09

Parth Raval


I already know a answer was submitted previously.

(Asynchronous JavaScript + XML)

Is a group of interrelated client- and server-side development technologies that allows parts of a webpage to be updated without having to reload the entire page think of sites like YouTube, Google Maps, Gmail, and tabs within Facebook. It changed usability and the speed of web applications with its innovative concept: asynchronously exchanging small amounts of data with the server behind the scenes, without affecting the rest of the page. XMLHttpRequest is just a implementation of ajax, The XMLHttpRequest object is used to exchange data with a server.

like image 31
Remario Avatar answered Sep 28 '22 21:09

Remario