luisquike
9/18/2017 - 8:22 PM

Ajax

Ajax requests syntaxis examples

//POST sending and reciving data
$.ajax({
    type: "POST", //Type of request. 
    url: 'URL/api/3.1/get_resources', //URL to send the request
    contentType: 'application/json', //ContentType used to send data
    dataType: 'json',  //dataType expected
    data: JSON.stringify({id: id, object:{property: property}}), //Data to sent
    success: function(object) {
        //Request succeeds
    },
    error: function (xhr, ajaxOptions, thrownError) {
        //request fails
        //xhr.responseText
    } 
});


//PUT sending and reciving data
$.ajax({
    type: "PUT", //Type of request. 
    url: 'URL/api/3.1/resource.json', //URL to send the request
    contentType: 'application/json', //ContentType used to send data
    dataType: 'script',  //dataType expected
    data: JSON.stringify({id: id, object:{property: property}, _method:'put'}), //Data to sent
    success: function(data) {
        //Request succeeds
    },
    error: function (xhr, ajaxOptions, thrownError) {
        //request fails
        //xhr.responseText
    } 
});

//GET sending and reciving data
$.ajax({
    type: "GET", //Type of request. 
    global: false, //Boolean whether or not to trigger global AJAX event handles
    url: 'URL/api/3.1/resource', //URL to send the request
    contentType: 'application/json', //ContentType used to send data
    dataType: 'script',  //dataType expected
    data: JSON.stringify({}), //Data to sent
    success: function(data) {
        //Request succeeds
    },
    error: function (xhr, ajaxOptions, thrownError) {
        //request fails
        //xhr.responseText
    } 
});