Ajax

Handles API calls asynchronously, making HTTP requests and handling success/error callbacks.



How to Use

You can send just the url, as follows:

__.ajax.get('/endpoint');

Or you can send an wrapper object with 4 inner options: An url string along with params, headers and data objects.

__.ajax.get({
    url: '/endpoint',
    params: {
        'page': 1
    },
    headers: {
        'Content-Type': 'application/json'
    },
    data: {
        // ...
    }
});

The response is automaticaly parsed as JSON when returned. You got two callbacks, as follows. Success/error callbacks will return the xhr.responseText as arguments one and the full xhr as arguments two.

__.ajax.get()
.success(function (data, xhr) {
    // ...
}).error(function (data, xhr) {
    // ...
});

Methods


__.ajax.get( options: obj/string )

Sends a GET request to the server.

__.ajax.get('/endpoint');

__.ajax.post( options: obj/string )

Sends a POST request to the server.

__.ajax.post({
    url: '/endpoint',
    data: {
        // ...
    }
}).

__.ajax.put( options: obj/string )

Sends a PUT request to the server.

__.ajax.put({
    url: '/endpoint',
    data: {
        // ...
    }
})

__.ajax.delete( options: obj/string )

Sends a DELETE request to the server.

__.ajax.delete('/endpoint');

Based on atomic.js and the Ajax module from our previous library.