Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
Eser Ozvataf edited this page May 21, 2015 · 6 revisions

Enabling CORS support for IE

$l.ajax.corsDefault = true;

Making a GET request

Usage: $l.ajax.get(path[, values, cors])

// url: /listUsers 
$l.ajax.get('/listUsers').done(
    function(response) {
        console.log(response);
    }
);
// url: /getUser?userId=5&allDetails=true
$l.ajax.get(
    '/getUser',
    { userId: 5, allDetails: true }
).done(
    function(response) {
        console.log(response);
    }
);

Making a GET request with JSON response

Usage: $l.ajax.getJson(path[, values, cors])

// url: /listUsers 
$l.ajax.getJson('/listUsers').done(
    function(response) {
        console.log(response);
    }
);

Making a POST request

Usage: $l.ajax.post(path[, values, cors])

$l.ajax.post(
    '/login',
    { username: 'me', password: 'xxx', rememberme: true }
).done(
    function(response) {
        console.log(response);
    }
);

Making a POST request with FormData

Usage: $l.ajax.post(path[, values, cors])

var formData = new FormData();
formData.append('fullname', 'Laroux Blackmore');
formData.append('profilepicture', fileInputElement.files[0]);

$l.ajax.post('/updateProfile', formData).done(
    function(response) {
        console.log(response);
    }
);

Making a POST request with JSON request/responses

Usage: $l.ajax.postJson(path[, values, cors])

$l.ajax.postJson(
    '/login',
    { username: 'me', password: 'xxx', rememberme: true }
).done(
    function(response) {
        console.log(response);
    }
);

Executing a Remote Script

Usage: $l.ajax.getScript(path[, values, cors])

$l.ajax.getScript(
    'http://www.foo.com/bar.js'
);

JSONP

Usage: $l.ajax.getJsonP(path, values, method[, cors])

function getMemberData(data) {
    console.log(data);
}

$l.ajax.getJsonP(
    'http://www.foo.com/bar.jsonp',
    { memberId: 5 },
    'getMemberData'
);

Making detailed requests

Usage: $l.ajax.makeRequest({type: type, url: url, datatype: datatype, getdata: getdata, postdata: postdata, postdatatype: postdatatype, header: headers, cors: cors, jsonp: jsonp})

$l.ajax.makeRequest({
    type:         'POST',
    url:          '/ajax-request-url',
    datatype:     'json',
    postdata:     { username: 'me', password: 'xxx', rememberme: true },
    postdatatype: 'form',
    cors:         true,
    headers:      { 'X-Requested-With': 'XMLHttpRequest' }
}).done(
    function(response) {
        console.log('success', response);
    }
).fail(
    function(err) {
        console.log('failed', err);
    }
);
Clone this wiki locally