Skip to content

Ajax

Defined in: utilities/ajax/ajax.ts:196

Makes requests with ease.

  • Supports GET, POST, PUT, DELETE and JSONP.
  • The request is sent as soon as you create the object. There is no separate init() or send().
  • For JSONP, set up a global callback function first and pass its name as a string in jsonpCallback. success and error are not called for JSONP.
  • The page must be served over http(s). It throws when opened from a file:// URL.

GET request

new Ajax({
responseType: 'json',
url: 'https://reqres.in/api/users/2',
success: (xhr) => {
console.log(xhr.response.data);
},
error: (xhr) => {
console.log('ERROR', xhr);
}
});

POST request

new Ajax({
url: 'https://reqres.in/api/users',
method: 'POST',
responseType: 'json',
data: {
name: 'john smith',
age: 22
},
success: (xhr) => {
console.log(xhr.response);
},
error: (xhr) => {
console.log('ERROR', xhr);
}
});

JSONP request

(window as any).callbackFunction = (response: unknown) => {
console.log(response);
};
new Ajax({
url: 'https://jsfiddle.net/echo/jsonp',
method: 'JSONP',
data: {
name: 'john smith',
age: 22
},
jsonpCallback: 'callbackFunction'
});

new Ajax(settings): Ajax

Defined in: utilities/ajax/ajax.ts:212

Checks the settings and sends the request.

AjaxOptions

The options. url is required.

Ajax

If the page is not served over http(s), the URL is missing or empty, or cors is set in a browser that cannot do it.

readonly xhr: XMLHttpRequest | null = null

Defined in: utilities/ajax/ajax.ts:201

The XMLHttpRequest that made the request, so a caller can abort() it or read it later. null for JSONP, which does not use one.