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
Properties
1Sets 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
Functions
4Fetch the specified resource using the fetch API.
| Parameter | Type | Description |
|---|---|---|
url | String | URL to fetch from |
options | FetchOptions | The options for the |
The fetch Promise, which can be aborted by calling a special abort method
Make a request (using GET) to the specified url.
| Parameter | Type | Description |
|---|---|---|
url | String | URL to |
options | FetchOptions | The options for the |
The fetch Promise, which can be aborted by calling a special abort method
Registers the passed URL to return the passed mocked up Fetch Response object to the AjaxHelper's promise resolve function.
| Parameter | Type | Description |
|---|---|---|
url | String | The url to return mock data for |
response | Object | function | A mocked up Fetch Response object which must contain
at least a |
response.responseText | String | The data to return. |
response.synchronous | Boolean | resolve the Promise immediately |
response.delay | Number | resolve the Promise after this number of milliseconds. |
POST data to the specified URL.
| Parameter | Type | Description |
|---|---|---|
url | String | URL to |
payload | String | Object | FormData | The data to post. If an object is supplied, it will be stringified |
options | FetchOptions | The options for the |
The fetch Promise, which can be aborted by calling a special abort method
Typedefs
1
Typedefs
1Options 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 };
| Parameter | Type | Description |
|---|---|---|
method | GET | POST | PUT | PATCH | DELETE | The request method, e.g., |
queryParams | Object | A key-value pair Object containing the params to add to the query string |
headers | Object | Any headers you want to add to your request, contained within a |
body | Object | Any body that you want to add to your request: this can be a |
addQueryParamsToBody | Boolean | Indicates whether When the argument is
Otherwise, |
mode | cors | no-cors | same-origin | The mode you want to use for the request, e.g., |
credentials | omit | same-origin | include | The request credentials you want to use for the request:
|
parseJson | Boolean | Specify |
abortController | AbortController | Provide abort controller to be able to cancel fetch |