2011-01-30 94 views
1

我有一个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

怎么回事?

回答

0

http://api.jquery.com/jQuery.ajax/ 它看起来像你的文档中使用“失败”你有“错误”: 错误(XMLHttpRequest参数url errorThrown) 也应该做这样的事情:

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; 
       endFunction(aid,function(endv){ 
        if (endv) { 
        // do some other stuff 
        } 
       }); 
      } 
     }, 
     error: function (errorMsg) { alert(errorMsg.toString()); } 
    }); 
} 

function endFunction(aid,endcallback) { 
    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()); 
      endcallback(hasEnded); 
     }, 
     error: function(XMLHttpRequest, textStatus, errorThrown) { 
      console.log(textStatus, errorThrown); 
      endcallback("?"); 
     } 
    }); 
    //return hasEnded; 
} 
+0

谢谢为答案! 试过上面的代码,但是当我运行firstAjaxCall时,我得到“endcallback不是函数”。我想我需要更多关于这个回调事件的指导...... – 2011-02-01 19:16:40

2

AJAX是异步$.ajax调用不会等待服务器回复。

因此,return hasEnded;行在AJAX回调之前运行。

您需要让您的endFunction采取回调参数,如$.ajax那样。