我有一个JavaScript函数,它使用jQuery对Web服务进行JSON调用。 在成功函数中,我需要评估JSON响应,并在必要时在同一个Web服务中调用另一种方法。函数调用ajax返回false
这是我如何做到这一点:
function firstAjaxCall(aid, tid) {
$.ajax({
type: "POST",
contentType: "application/json;charset=utf-8",
url: "/webservices/Webservice.asmx/Method1",
data: "{ auctionId: '" + aid + "'}",
dataType: "json",
success: function (response) {
var respData = response.d;
//do some stuff
if (respData.HasEnded == true) {
clearInterval(tid);
var end = false;
end = endFunction(aid);
if (end) {
// do some other stuff
}
}
},
failure: function (errorMsg) { alert(errorMsg.toString()); }
});
}
并正在从阿贾克斯成功函数中调用的endfunction这样:
function endFunction(aid) {
var hasEnded = false;
$.ajax({
type: "POST",
contentType: "application/json;charset=utf-8",
url: "/webservices/Webservice.asmx/Method2",
data: "{ auctionId: '" + aid + "'}",
dataType: "json",
success: function (callbackData) {
var endRespData = callbackData.d;
hasEnded = endRespData.Success;
alert(hasEnded.toString());
},
failure: function(XMLHttpRequest, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
return hasEnded;
}
这里是怪异的东西。阿贾克斯呼吁做得很好。服务器上的代码按计划运行。但是,如果我尝试在endFunction(aid)
的成功函数中设置一个萤火虫断点,则不会命中,但会显示警告框,显示单词true。这有点好,因为我们似乎确实达到了成功的功能。然而,hasEnded
变量从未设置为true,因此它总是返回false。 从Firebug控制台调用endFunction(1)
将显示一个警告框,其中包含单词true
,并返回值false
。
怎么回事?
谢谢为答案! 试过上面的代码,但是当我运行firstAjaxCall时,我得到“endcallback不是函数”。我想我需要更多关于这个回调事件的指导...... – 2011-02-01 19:16:40