how to combine post and get the method with jquery?

advertisements

I have two jquery function get and post.

I retrieve data from get method first and post my data with post method.

I want to know how to combine this two function.

POST Method

var url = '/api/sample?id=' + id ;
AJAXnotification("Saving...", 'info');
$.post( url , form)
.done(function(){
    AJAXnotification("Saved", 'success', 5000);
})
.fail(function() {
    AJAXnotification("Cannot save", 'error');
});

GET Method

AJAXnotification("Loading...", 'info');

var fail_callback = function() {
    AJAXnotification("Cannot load ", 'error');
}

$.get('/api/samples', {
    'id'      : id,
    'merge'   : 'departures',
    'departures.from_date': from_date,
    'departures.to_date'  : to_date
}).done(function( tours ){
    AJAXnotification(" loaded", 'success', 5000);
});


Just embedding the $.post inside the .done function of your $.get will make the $.post fire after the $.get returns, and you'll be able to access all of the returned data:

$.get('/api/samples', {
    'id'      : id,
    'merge'   : 'departures',
    'departures.from_date': from_date,
    'departures.to_date'  : to_date
}).done(function( tours ){
    AJAXnotification(" loaded", 'success', 5000);

    var url = '/api/sample?id=' + id ;
    AJAXnotification("Saving...", 'info');
    $.post( url , form)
    .done(function(){
        AJAXnotification("Saved", 'success', 5000);
    })
    .fail(function() {
        AJAXnotification("Cannot save", 'error');
    });
});