var xmlHttp = new XMLHttpRequest();
The XMLHttpRequest object provides full access to HTTP, allowing you to send POST and HEAD requests, as well as standard GET requests. The XMLHttpRequest object can synchronously or asynchronously return a response from the web server in the form of text or a DOM document.
API | Description |
|---|---|
Specifies the response processing function. | |
Initializes HTTP request parameters. | |
Sets the request header. | |
Sends an HTTP request using the parameters passed to the open() method and an optional request body passed to the send method. | |
Returns all response headers. | |
Returns the value of the specified HTTP response header. | |
Cancels the current request. | |
Returns the response body (excluding the headers) received by the server so far, or an empty string if no data has been received. | |
Returns a Document object containing the XML retrieved by the request. | |
Parses the response into XML and returns the result as a Document object. | |
Returns the HTTP status code of a request. | |
Describes the status of an HTTP request. | |
Returns an ArrayBuffer, Blob, Document, or DOMString, depending on the value of XMLHttpRequest.responseType. | |
Defines enumerated values of the response type. | |
Returns a serialized response URL. If the URL is empty, an empty string is returned. | |
Returns an unsigned long integer representing the maximum time, in milliseconds, that the request is allowed to take. If this time is exceeded, the request is automatically terminated. | |
Returns a boolean value, indicating whether the cross-domain Access-Control request should contain authorization information, such as cookies or authorization headers. | |
Overrides the MIME type returned by the server. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
method | string | Yes | HTTP request method. |
url | string | Yes | Request body. |
async | boolean | No | Indicates whether the request should be executed asynchronously. The default value is true. |
username | string | No | User name for accessing the specified URL. |
password | string | No | Password for accessing the specified URL. |
var xhr = new XMLHttpRequest();
xhr.open('GET', '" + "test.php"+ "', true); Parameter | Type | Mandatory | Description |
|---|---|---|---|
name | string | Yes | Name of the header to be set. |
value | string | Yes | Header value. |
var xhr = new XMLHttpRequest();
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); Sends an HTTP request using the parameters passed to the open() method and an optional request body passed to the send method.
Parameter | Type | Mandatory | Description |
|---|---|---|---|
body | string/document | No | String or document. |
var xhr = new XMLHttpRequest();
xhr.open('POST', '" + "test.php"+ "', true);
xhr.send("foo=bar&lorem=ipsum"); Parameter | Type | Mandatory | Description |
|---|---|---|---|
header | string | Yes | Name of the HTTP response header to be returned. |
var client = new XMLHttpRequest();
client.open("GET", "unicorns-are-teh-awesome.txt", true);
client.send();
client.onreadystatechange = function() {
if(this.readyState == this.HEADERS_RECEIVED) {
console.log(client.getResponseHeader("Content-Type"));
}
}