Get the response from ajax before the if statement
Get the response from ajax before the if statement
misteam
Posts: 50Questions: 18Answers: 0
Hi everyone,
I need the get the value of pro_status, but if statement run first before the ajax, so the pro_status always result as null. Please help.
Thank you.
editorv2.on( 'initEdit', function ( e, node, data ) {
var ps_transaction_id = data.transactions.id;
var ps_app_level = data.transactions.approval_level;
var ps_type_ap = data.transactions.type_ap;
var ps_qa_status = data.transactions.qa_status;
editorv2.on( 'initSubmit', function ( e, node, data ) {
$.ajax({
type: 'POST',
url: "../pages/get_token_proclb.php",
dataType: 'text',
success: function(token) {
proclb_check_status(token,ps_transaction_id);
console.log("1. PROCLB Status Check (START)");
},
error: function(d){
alert(JSON.stringify(d))
}
});
var pro_status;
function proclb_check_status(token,transaction_id){
var obj = new Object();
obj.token = token;
obj.transaction_id = transaction_id;
$.ajax({
type: 'POST',
url: "../pages/check_proclb_status.php",
data: obj,
dataType: 'json',
success: function(resultData) {
if(resultData.succes == true){
pro_status = resultData.status;
}else{
pro_status = "";
}
},
error: function(d){
alert(JSON.stringify(d))
}
});
}
if(pro_status !== "Received"){
if (
(ps_app_level == "Quality Assurance" && ps_type_ap == "For Review" && (ps_qa_status == "For Review QA" || ps_qa_status == "For Review Other QA")) ||
(ps_app_level == "Team Leader" && ps_type_ap == "For Review" && (ps_qa_status == "For Review TL" || ps_qa_status == "For Review Other TL")) ||
(ps_app_level == "Operations Manager" && ps_type_ap == "For Review" && ps_qa_status == "For Review OM") ||
(ps_app_level == "Head Manager" && ps_type_ap == "For Review" && ps_qa_status == "For Review Head")
) {
swal("You can't approve this document","","warning" );
return false;
}
}
});
});
Answers
Hi @colin, please help.
You need to remember for the the first
a
inajax
stands for: Asynchronous.Your Ajax call happens in the background. Meanwhile the rest of the process continues, then when the Ajax call is done, it's
success
function is called.Have a look at the third example on the
initSubmit
documentation page to see how to handle this.Allan