Class: Ajax

mindsmine. Ajax

The Ajax class encapsulates an HTTP connection to the page's originating domain, allowing requests to be made to a URL specified at request time.


new Ajax()

Since:
  • 1.0.0

Members


<static, constant> ALLOWED_METHODS

Provides an array of allowed HTTP methods.

Since:
  • 1.0.0

<static, constant> DEFAULT_ASYNC

Provides the default async value for the Ajax calls.

Since:
  • 1.0.0

<static, constant> DEFAULT_SCOPE

Provides the default scope value for callbacks.

Since:
  • 1.0.0

<static, constant> DEFAULT_TIMEOUT

Provides the default timeout (in milliseconds) for the Ajax calls.

Since:
  • 1.0.0

<static, constant> DEFAULT_WITH_CREDENTIALS

Provides the default withCredentials value for Ajax calls.

Since:
  • 1.0.0

<static, constant> XHRObject

Returns the Ajax object based upon the browser.

Since:
  • 1.0.0

Methods


<static> request(method, url, options)

Sends an HTTP request to a remote server.

Requests made by this method are by default asynchronous, and will return immediately. No data from the server will be available to the statement immediately following this call. To process returned data, use a success (or afterRequest) callback in the request options object.

Example Usage:

 mindsmine.Ajax.request(
      "GET",
      "valid URL",
      {
           headers: {
                "Accept" : "some value",
                "Content-Type" : "some value",
                "Authorization" : "some value"
           },

           success: function (request) {
                var responseJSON = JSON.parse(request.responseText);
                console.log(responseJSON);
           },

           failure: function (request) {
                console.log("HTTP error " + request.status);
           }
      }
 );
Parameters:
Name Type Description
method String

The HTTP method to use for the request. Note that the method name is case-sensitive and should be all caps.

url String

The URL to which to send the request.

options Object

An object which may contain the following properties:

Properties
Name Type Argument Default Description
async Boolean <optional>
true

true if this request should run asynchronously. false if this request should run synchronously (it will cause the UI to be blocked, the user won't be able to interact with the browser until the request completes).

headers Object <optional>

Request headers to set for the request.

scope Object <optional>
window

The scope in which to execute the callbacks (refers to the "this" parameter for the callback functions).

jsonData Object | String <optional>

JSON data to use as the post.

withCredentials Boolean <optional>
false

true to add the withCredentials property to the XHR object.

beforeRequest function <optional>

The function to be called before a network request is made to retrieve a data object.

afterRequest function <optional>

The function to be called upon receipt of the HTTP response. This function is called regardless of success or failure. The callback is passed the following parameters:

Properties
Name Type Description
response XMLHttpRequest

The XMLHttpRequest object containing the response data. See XMLHttpRequest for details about accessing elements of the response.

success function

The function to be called upon success of the request. The callback is passed the following parameters:

Properties
Name Type Description
response XMLHttpRequest

The XMLHttpRequest object containing the response data. See XMLHttpRequest for details about accessing elements of the response.

failure function

The function to be called upon failure of the request. The callback is passed the following parameters:

Properties
Name Type Description
response XMLHttpRequest

The XMLHttpRequest object containing the response data. See XMLHttpRequest for details about accessing elements of the response.

Since:
  • 1.0.0
Throws:

If invalid arguments.

Type
TypeError