Configuration - mrred85/ajax-request GitHub Wiki
Sample with default values:
ajax('url', {
method: 'GET',
type: 'text',
timeout: 0,
data: null,
async: true,
headers: null,
getState: function (state) {
console.log(state);
},
progress: function (ev) {
console.info(ev.loaded, ev.total);
},
success: function (response) {
console.log(response);
},
fail: function (code, message) {
console.warn(code, message);
},
error: function (code, message) {
console.error(code, error);
}
});
Options
method
Request method type
Type: string. Defaults to GET
.
Values: GET, POST, PUT, PATCH, DELETE.
type
Returns the response type
Type: string. Defaults to text
.
Values: text, arraybuffer, blob, document, json
timeout
Can be set to a time in milliseconds
Type: numeric. Defaults to 0
.
When set to a non-zero value will cause fetching to terminate after the given time has passed. When the time has passed, the request has not yet completed, and the synchronous flag is unset, a timeout event will then be dispatched, or a "TimeoutError" DOMException will be thrown otherwise (for the send() method).
data
to be sent
Data types supported:
string
: name=John&age=18object
: {name: 'John', age: 18}
Defaults to null
.
async
synchronous flag
Type: boolean. Defaults to true
.
Values: true
or false
.
headers
Set custom headers
Type: Object. Defaults to null
.
An object of additional header key/value pairs to send along with requests using the XMLHttpRequest transport. The header X-Requested-With: XMLHttpRequest
is always added.
Example:
headers: {
'Content-Type': 'text/html; charset=UTF-8',
'Content-Language': 'de-DE',
'Accept': 'text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8'
}
getState
Type: function(state: number)
Returns client's state.
progress
Type: function(event: ProgressEvent)
The function is called periodically with information when an XMLHttpRequest before success completely.
Event:
event.loaded
the amount of data currently transfered.event.total
the total amount of data to be transferred.
success
Type: function(response: any)
The function is called when an XMLHttpRequest transaction completes successfully.
The XMLHttpRequest response
property returns the response's body content as an ArrayBuffer
, Blob
, Document
, JavaScript Object
, or DOMString
, depending on the value of the request's responseType
property.
fail
Type: function(code: number, message: string)
The function is called when an XMLHttpRequest transaction does not complete successfully.
Parameters:
code
HTTP status code of the response.message
containing the response's status message as returned by the HTTP server.
error
Type: function(code: number, message: string)
The function is called when an XMLHttpRequest transaction fails due to an error.
Parameters:
code
HTTP status code of the response.message
containing the response's status message as returned by the HTTP server.