Is there any way to have a $.post within a success callback of a $.post again. If the first $.post is not successful i will ask for prompt. If confirmed it call perform another $.post again
my code is as below
postvars = {
order_no:order_no,
tracking_no:tracking_no,
continue_submit: false
};
url = '../shipment/shipment_submit_order_status_ajax';
$.post(url, postvars, function(data1) {
if(data1.status == "success")
{
alert("Order submitted 1");
}
else if(data1.status != "success")
{
var r = confirm("The Order is NOT ready to be shipped. Please check the order again. Are you sure you want to continue to ship this?");
if(r)
{
//Submit order
postvars = {
order_no:order_no,
tracking_no:tracking_no,
continue_submit: true
};
url = '../shipment/shipment_submit_order_status_ajax';
$.post(url, postvars, function(data) {
if(data.status == "success")
{
alert("Order submitted 2");
}
}, 'json');
}
}
}, 'json');
it is executing both calls instead of prompting a confirm dialog first before executing the 2nd ajax call. Anyone has any solution? Thanks in advance
Instead of 'if(r)' use 'if(true===r)'
Because in javascript 'if(r)' means whether the variable 'r' is defined. In this case this variable 'r' is defined and therefore the 'if' part is satisfied.
I recommend to you using deffered objects in jQuery:
function ajaxMe(data){
var df = $.Deferred();
$.post('my_const_url', data, function(resp){
if(resp.status !== 'success'){
df.resolve(other_data);
}else{
df.reject(other.data);
}
}, 'json');
return df.promise();
}
ajaxMe(postvars).done(function(response_data){
ajaxMe(whatever_you_want);
}).fail(function(){
var r = confirm("Do you really want it ?");
if(r){
ajaxMe(some_data);
}
});