PDA

View Full Version : The Ext.ajax Call not running in order.



daniel_82
24 Mar 2011, 10:06 PM
:-/I use Ext.Ajax.request to submit request and get data back from server.
As the code blow, the order of running those scripts are CALL 1 -> CALL 2 -> CALL 3

CALL 2 is nested inside CALL 1, CALL 2 need to call a funtion with has CALL 3

Most of time they are working OK with those order, however, sometime the javascipt just run CALL1 and jump CALL 2 and CALL3, and sometimes it juse run CALL 1 and CALL 2m but jump CALL3.

And I set call the ajax request with async: false. But still no luck.

Anyone know what's wrong with my script?

Thanks:-/




//save retail invoice transactions
function SaveInvoice(rowsData, MyCompleteClass, SalesOrReturnP) {

var rtnReq = { CustomerP: CustomerP, EmployeeP: EmployeeP,
StoreP: StoreP, ReasonP: ReasonP,
Notes: Notes, RetailOrderP: RetailOrderV,
ExchangeRateP: ExchangeRateV, RoundingAmountP: RoundingAmountV,
ChangeAmountP: ChangeAmountV, MoneyAmountP: MoneyAmount,
TenderP: Ext.util.JSON.encode(TenderP),
itemsData: Ext.util.JSON.encode(itemsData),
SalesOrReturnP: SalesOrReturnP
};
// CALL 1
Ext.Ajax.request({
url: '/POS/PosServices.svc/RecordRetailInvoice',
method: "POST", async: false,
params: Ext.util.JSON.encode(rtnReq),
callback: function(options, success, response) {
if (success) {
var tmp = Ext.util.JSON.decode(response.responseText);
var responseArray = Ext.util.JSON.decode(tmp.RecordRetailInvoiceResult);
if (responseArray.success == true) { //exists
//for printing
var printDocketNo = responseArray.data[0].PrintDocketNo;
........

if (printDocketNo != "NoPrint") {
var rtnReqV = { DocketNo: printDocketNo };
//CALL 2
Ext.Ajax.request({
url: '/POS/PosServices.svc/GetRedeemedDataListing',
method: "POST", async: false,
params: Ext.util.JSON.encode(rtnReqV),
callback: function(options, success, response) {
if (success) {
var tmp = Ext.util.JSON.decode(response.responseText);
var redeemData = Ext.util.JSON.decode(tmp.GetRedeemedDataListingResult);

if (redeemData != "") {
//print here
PrintSalesDockets(printDocketNo, MoneyAmount, ChangeAmountV, TenderP, itemsData, redeemData, "SALES", "NO");
}
else {
PrintSalesDockets(printDocketNo, MoneyAmount, ChangeAmountV, TenderP, itemsData, redeemData, "GVCNSALES","NO");
}
}
else {
Ext.MessageBox.hide();
Ext.Msg.show('Message', 'Try again or contact RMCS');
}
}, timeout: 600000
});
}
}
}
},
failure: function(response, options) {
Ext.MessageBox.hide();
Ext.MessageBox.alert('Failed', 'Unable to complete');
},
timeout: 600000
});
}

//print all sales related dockets
function PrintSalesDockets(printDocketNo, MoneyAmount, ChangeAmountV, TenderData, itemsData, redeemData,SalesType,Reprint) {
var reqV = { DocketNo: printDocketNo };
//CALL 3
Ext.Ajax.request({
url: '/POS/PosServices.svc/GetPrintDocketDetails',
method: "POST", async: false,
params: Ext.util.JSON.encode(reqV),
callback: function(options, success, response) {
if (success) {
// do something here
........
} else {
Ext.MessageBox.alert("failed", response.responseText);
}
},
failure: function(response, options) {
Ext.MessageBox.alert('Failed', 'Please try again.');
}
});
}

quangnd
26 Mar 2011, 7:21 AM
The problem happens for sure :D because Ext.Ajax run in asynchronous manner using callback.

daniel_82
27 Mar 2011, 2:12 PM
even I set "async: false" for each Ext.Ajax?
Are there any way to get around this problem?
Thanks

daniel_82
27 Mar 2011, 6:51 PM
any clue?:-/

daniel_82
28 Mar 2011, 2:11 PM
need help ASAP=D>

tvanzoelen
29 Mar 2011, 1:57 AM
What do you mean by Jumping? Is it that CALL 2 and CALL 3 are not executed? In that case it has nothing to do with order, but the code in the callback fails somewhere. That can happen if it is not possible to decode the response like:


var tmp = Ext.util.JSON.decode(response.responseText);

Install firebug and set a breakpoint on the callback.

daniel_82
30 Mar 2011, 4:32 PM
What do you mean by Jumping? Is it that CALL 2 and CALL 3 are not executed? In that case it has nothing to do with order, but the code in the callback fails somewhere. That can happen if it is not possible to decode the response like:


var tmp = Ext.util.JSON.decode(response.responseText);

Install firebug and set a breakpoint on the callback.


I don't think there is something wrong with the code itself.
If the code was wrong, CALL 2 and CALL 3 will always can not be executed (I test with same input).
However, in my case is most of time it running as CALL1, CALL2 and then CALL3, but sometime it just run CALL 1 and jump CALL2 and CALL3.

"CALL 3" is nested into "CALL 2" callback and "CALL 2" is nested into "CALL 1" callback. It looks like sometime "CALL 1" does not wait "CALL2" to finish, then execute next.

daniel_82
4 Apr 2011, 4:44 PM
any ideas, still waiting.~o)~o)~o)