Ajax
Defined in: utilities/ajax/ajax.ts:196
Makes requests with ease.
- Supports
GET,POST,PUT,DELETEandJSONP. - The request is sent as soon as you create the object. There is no separate
init()orsend(). - For
JSONP, set up a global callback function first and pass its name as a string injsonpCallback.successanderrorare not called forJSONP. - The page must be served over http(s). It throws when opened from a
file://URL.
Examples
Section titled “Examples”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'});Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Ajax(
settings):Ajax
Defined in: utilities/ajax/ajax.ts:212
Checks the settings and sends the request.
Parameters
Section titled “Parameters”settings
Section titled “settings”The options. url is required.
Returns
Section titled “Returns”Ajax
Throws
Section titled “Throws”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.
Properties
Section titled “Properties”
readonlyxhr: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.