Table of Contents

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

ParameterTypeDescription
options.
Required
ObjectAjax options. Has the following keys:
URL
Required
StringThe URL to access.
“method”
Required
StringThe HTTP medhod. Ex. GET, POST, PUT.
“data”ObjectData to send with the request.
“headers”ObjectThe object that contains headers to send with the request. Each item is passed to XMLHttpRequest.setRequestHeader().

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, options)

Type:Undefined Asynchronous Static

Loads the script file from the server. The loaded script will be appended to the current page and executed.

Parameters

ParameterTypeDescription
url
Required
StringThe URL of the file.
optionsStringLoad options. Has the following keys:
“type”StringThe type of a script. Used as a value for the type attribute.

Return Value

Undefined.


loadJSON(url, options)

Type:Object Asynchronous Static

Loads the JSON/Javascript object file from the specified URL. The loaded file will be converted to a Javascript object. You can specify whether the file is a JSON or Javascript object in the options parameter. If the format is not specified, the file is considered to be JSON if the extension is “json” and a Javascript object if the extension is “js”.

Parameters

ParameterTypeDescription
url
Required
StringThe URL of the file.
optionsStringLoad options. Has the following keys:
“format”StringThe file format. “js” or “json”.

Return Value

The Javascript object that is converted from the content of the retrieved file.


loadText(url)

Type:String Asynchronous Static

Loads the text file from the specified URL.

Parameters

ParameterTypeDescription
url
Required
StringThe URL of the file.

Return Value

The content of the retrieved file.


loadHTML(url)

Type:String Asynchronous Static

Loads the HTML file from the specified URL.

Parameters

ParameterTypeDescription
url
Required
StringThe URL of the file.

Return Value

The content of the retrieved file.


loadCSS(url)

Type:String Asynchronous Static

Loads the CSS file from the specified URL.

Parameters

ParameterTypeDescription
url
Required
StringThe URL of the file.

Return Value

The content of the retrieved file.


loadClass(url, options)

Type:Undefined Asynchronous Static

Loads the class files from the specified URL. The loaded class files will be appended to the current page and executed.

Do not add an extension to the URL argument. The library will automatically add the extension “.js” and load it. If the “splitClass” option is true, two files will be loaded, first “.js” and then “.settings.js”.

Parameters

ParameterTypeDescription
url
Required
StringThe URL of the file without extension.
optionsStringLoad options. Passed to loadScript().

Return Value

Undefined.