AjaxUtil
Overview
AjaxUtil is a utility class that uses XMLHttpRequest to send an ajax request to a server.
Methods
ajaxRequest(options)
Type:XMLHttpRequest
Asynchronous
Static
Sends an ajax request to a server.
Parameters
Parameter | Type | Description |
---|---|---|
options | Object | Ajax options. Valid keys are: |
“url” | String | A URL to access. |
“method” | String | An HTTP medhod. Ex. GET, POST, PUT |
“data” | Object | Data to send with the request. |
“headers” | Object | An object that contains headers to send with the request. Each item is passed to XMLHttpRequest.setRequestHeader(). |
“options” | Object | Ajax options. Passed to XMLHttpRequest. |
Return Value
XHR Object.
Examples
GET
let url = "https://api.example.com/members/1/" let method = "GET"; BITSMIST.v1.AjaxUtil.ajaxRequest({url:url, method:method}).then((xhr) => { console.log(xhr.responseText); });
POST
let url = "https://api.example.com/members/" let method = "POST"; let item = {"id":2, "name":"John"}; let headers = { "Content-Type":"application/json", "X-From": "www.example.com" }; let options = {"withCredentials":true}; BITSMIST.v1.AjaxUtil.ajaxRequest({ url:url, method:method, data:item, headers:headers, options:options }).then((xhr) => { console.log(xhr.responseText); });
loadScript(url)
Type:undefined
Asynchronous
Static
Loads a script file from the server and append it to the current page.
Parameters
Parameter | Type | Description |
---|---|---|
url | String | The URL of the file. |
Return Value
undefined