The Ajax class encapsulates an HTTP connection to the page's originating domain, allowing requests to be made to a URL specified at request time.
- Since:
- 1.0.0
- Deprecated:
- See fetch for the in-built Promise based solution.
Members
(static, constant) ALLOWED_METHODS
Provides an array of allowed HTTP methods.
- Since:
- 1.0.0
- Deprecated:
- See fetch for the in-built Promise based solution.
(static, constant) DEFAULT_ASYNC
Provides the default async
value for the Ajax calls.
- Since:
- 1.0.0
- Deprecated:
- See fetch for the in-built Promise based solution.
(static, constant) DEFAULT_TIMEOUT
Provides the default timeout
(in milliseconds) for the Ajax calls.
- Since:
- 1.0.0
- Deprecated:
- See fetch for the in-built Promise based solution.
(static, constant) XHRObject
Returns the Ajax object based upon the browser.
- Since:
- 1.0.0
- Deprecated:
- See fetch for the in-built Promise based solution.
Methods
(static) request(method, url, options) → {Promise}
Sends an HTTP request to a remote server and returns a Promise object. The Promise callback functions are passed the XMLHttpRequest object containing the response data. See XMLHttpRequest for details about accessing elements of the response.
Requests made by this method are by default asynchronous, and will return immediately. No data from the server will be available to the statement immediately following this call.
Example Usage:
mindsmine.Ajax.request(
"GET",
"valid URI",
{
headers: {
"Accept" : "some value",
"Content-Type" : "some value",
"Authorization" : "some value"
}
}
).then((response) => {
let responseJSON = JSON.parse(response.responseText);
console.log(responseJSON);
}).catch((response) => {
console.log(`HTTP error code = ${response.status}`);
}).finally(() => {
console.log("This function is called regardless of success or failure.");
});
Parameters:
Name | Type | Description | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
method |
String | The HTTP method to use for the request. Note that the method name is case-sensitive and should be all caps. |
|||||||||||||||||||||||||||||||||||
url |
String | The URL to which to send the request. |
|||||||||||||||||||||||||||||||||||
options |
Object | An object which may contain the following properties: Properties
|
- Since:
- 1.0.0
- Deprecated:
- See fetch for the in-built Promise based solution.
Throws:
-
If invalid arguments.
- Type
- TypeError
Returns:
A Promise that resolves to an XMLHttpRequest object.
- Type
- Promise