Request

The Request class facilitates communication with remote HTTP servers via the browser's XMLHttpRequest object or via JSONP. Its prefered to use this instead of XMLHttpRequest, this class will make sure there are no crossbrowser issues. In addition it also goes through a proxy to prevent Cross Domain issues.

new Request()

Configs

data : {Object}

Object containing data you want to send with the request.

Example
//When method is 'GET' or you did not define a method, then the data will be appended to the url.
 new Request({
    url: 'myurl.com', //Will be: 'myurl.com?id=1'
    data: { id: 1 }
 }).send();

 //Sending as json
 new Request({
    url: 'myurl.com',
    method: 'POST', //Anything but GET
    headers: {
       'Content-Type': 'application/json'
    },
    data: { id: 1 }
 }).send();

 //Sending in body
 new Request({
    url: 'myurl.com',
    method: 'POST', //Anything but GET
    data: { id: 1 }
 }).send();

headers : {Object}

Can be used to define the headers to use with the request.

jsonp : {Boolean}

Use true if you want to send the request as JSONP

method : {String}

Define which request method to use (GET, POST, DELETE, PUT). Default it will use GET.

onComplete : {Method}

Callback method always triggered when request is done.

NameTypeDescription
requestClass

The XMLHttpRequest

Example
new Request({
    url: 'myurl.com',
    onComplete: function (request) {
       if (request.status === 200)
          JSON.parse(json.response);
    }
 }).send({ id: 1 });

onError : {Method}

Callback method triggered when the request was successful but the result failed to parse.

NameTypeDescription
errorClass

Contains the error that occured.

onFailure : {Method}

Callback method triggered when the request failed.

NameTypeDescription
requestClass

The XMLHttpRequest

onSuccess : {Method}

Callback method triggered when the request was successful.

NameTypeDescription
resultMixed

The parsed data returned from the server depending on content-type.

requestClass

The XMLHttpRequest

onTimeout : {Method}

When a timeout config is used and the request takes to long, this callback will trigger.

NameTypeDescription
requestClass

The XMLHttpRequest

password : {String}

Authentication with password

proxy : {Boolean|Object}

Default true, use false to not use the proxy.

NameTypeArgumentDescription
jsonBooleanoptional 

Indicates if the proxy should convert the incoming data into JSON.

nocacheBooleanoptional 

Indicates if the proxy should cache the incoming data, enabling this could lead to slower data retrieval.

cookieBooleanoptional 

Adds X-Proxy-COOKIE as header to your request.

headersBooleanoptional 

Adds X-Proxy-HEADERS as header to your request.

user : {String}

Authentication with username

Methods

abort()

This will abort the request if it is pending.

send(data)

Parameters:

NameTypeDescription
dataObject

Data you want to send with the request. You can also define it in the config and not use it here.

Example

new Request({
    url: 'myurl.com'
 }).send({ id: 1 });

 var myRequest = new Request({
    url: 'myurl.com',
    data: { id: 1 }
 }).send();
 myRequest.send();