THE XMLHttpRequest OBJECT
Internet Explorer 5 was the first browser to introduce the XHR object. It did so through the use of an ActiveX object included as part of the MSXML library. As such, three versions of the XHR object may be used in the browser: MSXML2.XMLHttp
, MSXML2.XMLHttp.3.0
, and MXSML2.XMLHttp.6.0
.
All modern browsers support a native XHR object that can be created using the XMLHttpRequest
constructor as follows:
let xhr = new XMLHttpRequest();
XHR Usage
To begin using an XHR object, you will first call the method open()
, which accepts three arguments: the type of request to be sent ("get"
, "post"
, and so on), the URL for the request, and a Boolean value indicating if the request should be sent asynchronously. Here's an example:
xhr.open("get", "example.php", false);
This line opens a synchronous GET
request for example.php
. There are a couple of things to note about this code. First, the URL is relative to the page on which...