ajax
Sends an asynchronous HTTP request and returns its XMLHttpRequest. The answer arrives later, in the success or error function of the options.
General
ajax(data)
- data AjaxOptions
The request options.
- data.url string
The URL to send the request to.
- data.type string
HTTP method, such as
'get'or'post'. - data.data Document | XMLHttpRequestBodyInit | nulloptional
Request body, such as a form-encoded string (
'name=value&other=value') orFormData. Leave it out for a request without a body. - data.success (x: XMLHttpRequest) => voidoptional
Called with the
XMLHttpRequestwhen the request finishes with status 200; read the answer from itsresponseorresponseText. Default logs the request to the console. - data.error (x: XMLHttpRequest) => voidoptional
Called with the
XMLHttpRequestwhen the request finishes with any other status, including network errors (status 0). Default does nothing. - data.contentType string | falseoptional
Value of the
Content-Typeheader, orfalseto send none (needed forFormData). Default is'application/x-www-form-urlencoded'. - data.withCredentials booleanoptional
Set to
trueto send cookies and other credentials with a cross-site request. Default isfalse.
returns XMLHttpRequest
Sends an asynchronous HTTP request and returns its XMLHttpRequest. The answer arrives later, in the success or error function of the options.
THRUNIUM.ajax({
url: 'https://www.example.com/get-data',
type: 'get',
withCredentials: true,
success: function(request) {
console.log(request.responseText);
},
error: function(request) {
console.log('Request failed with status ' + request.status);
}
});THRUNIUM.ajax({
url: 'https://www.example.com/score',
type: 'post',
data: 'name=Ann&score=120',
success: function(request) {
console.log('Saved');
}
});