在我的jquery函数中,我有两个带有serverside方法的ajax调用,它的工作正常, 问题是ajax调用在ajax代码运行之后的最后一个amd但是这行代码depand on
function Rbook(b) {
var one = $(b).attr("data-oneislcc");
var two = $(b).attr("data-twoislcc");
var trip1 = $(b).attr("data-oneinfo");
var trip2 = $(b).attr("data-twoinfo");
var owflt = "l";
var inflt = 'r';
var owjdata = $(b).attr("data-ow");
var iwjdata = $(b).attr("data-iw");
var llccreturn, rlccreturn;
var lres, rres;
$("#fadeing").css("display", "block");
$("#fade").css("display", "block").css("height", $(document).height / 2);
if (one == 'true') {
$.ajax({
type: "POST",
url: "Search-RoundResult.aspx/FareQuoteMethod",
data: "{'ALcode':'" + trip1 + "','flt':'" + owflt + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function OnSuccess(response) {
if (response.d != null) {
lres = response.d;
if (response.d == "sessionExpire") {
}
else {
var data = new Array()
data = response.d.split("oldfare=");
llccreturn = owlcc(data[0], data[1])
}
}
else {
alert("Please Try agian.");
}
},
Error: function errer(msg) {
$("#fade").css("display", "none");
$("#light").css("display", "none");
alert(msg.d)
}
});
}
else {
llccreturn = ownonlcc(owjdata);
}
if (two == 'true') {
$.ajax({
type: "POST",
url: "Search-RoundResult.aspx/FareQuoteMethod",
data: "{'ALcode':'" + trip2 + "','flt':'" + inflt + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function OnSuccess(response) {
if (response.d != null) {
if (rres == "sessionExpire") {
}
else {
var data = new Array()
data = response.d.split("oldfare=");
rlccreturn = iwlcc(data[0], data[1])
}
}
else {
alert("Please Try agian.");
}
},
Error: function errer(msg) {
$("#fade").css("display", "none");
$("#light").css("display", "none");
alert(msg.d)
}
});
}
else {
rlccreturn = iwnonlcc(iwjdata);
}
if (llccreturn == 'farechange' || rlccreturn == 'farechange') {
$("#farechange").css("display", "block");
$("#fade").css("display", "block");
}
if (llccreturn == 'nofarechange' || rlccreturn == 'nofarechange') {
window.location = "reviewbooking.aspx?trip1=" + $(b).attr("data-oneinfo") + "&iwlcc=" + $(b).attr("data-oneislcc") + "&trip2=" + $(b).attr("data-twoinfo") + "&owlcc=" + $(b).attr("data-twoislcc");
}}
var owlcc = function (jdata, oldfare) {
//Some Calulation
retrun 'farechange';
}
var ownonlcc = function (jdata) { //Some Calulation
retrun 'nofarechange'}
var iwlcc = function (jdata, oldfare) { //Some Calulation
return 'farechange'}
var iwnonlcc = function (jdata) { retrun 'nofarechange'}
如果运行此代码,则运行此行代码然后休息,以便条件不验证 我不知道我在哪里做错了
if (llccreturn == 'farechange' || rlccreturn == 'farechange') {
$("#farechange").css("display", "block");
$("#fade").css("display", "block");
}
if (llccreturn == 'nofarechange' || rlccreturn == 'nofarechange') {
window.location = "reviewbooking.aspx?trip1=" + $(b).attr("data-oneinfo") + "&iwlcc=" + $(b).attr("data-oneislcc") + "&trip2=" + $(b).attr("data-twoinfo") + "&owlcc=" + $(b).attr("data-twoislcc");
}
答案 0 :(得分:1)
看起来你不了解异步javascript。当您执行ajax调用时,它会发送请求,然后继续运行代码,然后才运行成功处理程序。否则,在响应回来之前,它根本无法做任何事情,因为javascript是单线程的。
Rbook
函数执行以下操作:首先,从DOM获取大量信息;然后,发送一些ajax请求(并设置处理程序在响应返回时运行);然后可能会对DOM进行一些更改,具体取决于llccreturn
和rlccreturn
的值(但它们仍然是undefined
)。只有当ajax响应返回并且成功处理程序运行时才设置这些变量,但到那时为时已晚。
如果您想在听到ajax响应后运行一些代码,请将其放入成功处理程序(或从成功处理程序中调用它)。在这种情况下,由于您不希望运行代码直到两个 ajax响应都已到达,而且您可能只是运行它iwnonlcc
或ownonlcc
同步,而不是进行ajax调用。我说最简单的解决方法是将最终要运行的代码包装在一个函数和if
块中,如下所示:
function dataWasReceived() {
if (llccreturn !== undefined && rlccreturn !== undefined) {
if (llccreturn == 'farechange' || rlccreturn == 'farechange') {
$("#farechange").css("display", "block");
$("#fade").css("display", "block");
}
if (llccreturn == 'nofarechange' || rlccreturn == 'nofarechange') {
window.location = "reviewbooking.aspx?trip1=" + $(b).attr("data-oneinfo") + "&iwlcc=" + $(b).attr("data-oneislcc") + "&trip2=" + $(b).attr("data-twoinfo") + "&owlcc=" + $(b).attr("data-twoislcc");
}
}
}
然后,每次设置llccreturn
或rlccreturn
的值时,请调用此函数:
rlccreturn = iwlcc(data[0], data[1])
dataWasReceived();
和
rlccreturn = iwnonlcc(iwjdata);
dataWasReceived();
等
我也关注这一行(出现两次):
data: "{'ALcode':'" + trip2 + "','flt':'" + inflt + "'}",
您可能希望将参数应用为对象,而不是表示该对象的JSON字符串:
data: {ALcode: trip2, flt: inflt},
(顺便说一下,你提供的字符串无论如何都不是有效的JSON,因为它使用的是'
而不是"
。)