Browser - XMLHttpRequest (XHR) API

> (World Wide) Web - (W3|WWW) > Web - Browser

1 - About

The XMLHttpRequest is a Aysnchronous HTTP request designed to fetch resources.

The XMLHttpRequest ​specification defines ​an API that provides ​scripted ​client functionality for transferring data between a client and a server.

The XMLHttpRequest is used heavily in AJAX programming.

See also the new Browser - Web API - Fetch function

Advertising

3 - Example

function listener() {
  var data = JSON.parse(this.responseText);
  data.items.map(item => (
      console.log(item.name+" "+item.price)
  ));
}
 
function error(err) {
  console.log('Fetch Error', err);
}
 
var xhRequest= new XMLHttpRequest();
xhRequest.onload = listener;
xhRequest.onerror = error;
xhRequest.open('get', 'https://gerardnico.com/doc/json/items.json', true);
xhRequest.send();

4 - Management

4.1 - See

In the Browser devtool, you can see the request.

Example with Chrome.

chrome_devtool_xhr_fetch_request.jpg

HTTP - Header (Fields)

X-Requested-With=XMLHttpRequest

X-Requested-With cannot be added to a cross domain request without the consent of the server via CORS.

5 - Documentation / Reference

Advertising