AjaxHelper

Simplifies Ajax requests. Uses fetch & promises.

AjaxHelper.get('some-url').then(response => {
    // process request response here
});

Uploading file to server via FormData interface. Please visit FormData for details.

const formData = new FormData();
formData.append('file', 'fileNameToUpload');
AjaxHelper.post('file-upload-url', formData).then(response => {
    // process request response here
});

Properties

1

Sets default options for AjaxHelper#fetch() calls. Please see FetchOptions and fetch API for details.

// default content-type for all requests will be "application/json"
AjaxHelper.DEFAULT_FETCH_OPTIONS = {
    headers : {
        'content-type' : 'application/json'
    }
};

Functions

4
fetchstatic

Fetch the specified resource using the fetch API.

ParameterTypeDescription
urlString

URL to fetch from

optionsFetchOptions

The options for the fetch API

Returns: Promise -

The fetch Promise, which can be aborted by calling a special abort method

getstatic

Make a request (using GET) to the specified url.

ParameterTypeDescription
urlString

URL to GET from

optionsFetchOptions

The options for the fetch API

Returns: Promise -

The fetch Promise, which can be aborted by calling a special abort method

mockUrlstatic

Registers the passed URL to return the passed mocked up Fetch Response object to the AjaxHelper's promise resolve function.

ParameterTypeDescription
urlString

The url to return mock data for

responseObject | function

A mocked up Fetch Response object which must contain at least a responseText property, or a function to which the url and a params object and the Fetch options object is passed which returns that.

response.responseTextString

The data to return.

response.synchronousBoolean

resolve the Promise immediately

response.delayNumber

resolve the Promise after this number of milliseconds.

poststatic

POST data to the specified URL.

ParameterTypeDescription
urlString

URL to POST to

payloadString | Object | FormData

The data to post. If an object is supplied, it will be stringified

optionsFetchOptions

The options for the fetch API

Returns: Promise -

The fetch Promise, which can be aborted by calling a special abort method

Typedefs

1

Options for the requests. Please see fetch API for details

To set default values for the options please use DEFAULT_FETCH_OPTIONS property:

// enable passing parameters in request body by default
AjaxHelper.DEFAULT_FETCH_OPTIONS = { addQueryParamsToBody : true };
ParameterTypeDescription
methodGET | POST | PUT | PATCH | DELETE

The request method, e.g., GET, POST

queryParamsObject

A key-value pair Object containing the params to add to the query string

headersObject

Any headers you want to add to your request, contained within a Headers object or an object literal with ByteString values

bodyObject

Any body that you want to add to your request: this can be a Blob, BufferSource, FormData, URLSearchParams, or USVString object. Note that a request using the GET or HEAD method cannot have a body.

addQueryParamsToBodyBoolean

Indicates whether queryParams should be passed in the request body. Adding them to the body applies for application/x-www-form-urlencoded and multipart/form-data content types only, so make sure to pass corresponding Content-Type header to headers.

When the argument is true and:

  • if application/x-www-form-urlencoded content-type header is passed the method will make a URLSearchParams instance with queryParams and set it as the request body. And if body already has a URLSearchParams instance provided the parameters will be set there.
  • if multipart/form-data content-type header is passed the method will make a FormData instance with queryParams and set it as the request body. And if body already has a FormData instance provided the parameters will be set there.

Otherwise, queryParams are added to the query string.

modecors | no-cors | same-origin

The mode you want to use for the request, e.g., 'cors', 'no-cors', or 'same-origin'.

credentialsomit | same-origin | include

The request credentials you want to use for the request: 'omit', 'same-origin', or 'include'. To automatically send cookies for the current domain, this option must be provided

parseJsonBoolean

Specify true to parses the response and attach the resulting object to the Response object as parsedJson

abortControllerAbortController

Provide abort controller to be able to cancel fetch