使用jQuery从Ajax获取响应时出现延迟

问题描述:

我正在使用jQuery调用Web服务(c#)方法。从Web服务获取结果后,我必须以html形式绑定数据。但问题是在运行代码时,我迟到了,直到那时我的函数返回null并且没有设置表单域。下面是 是我的代码。在这里,我得到空值乞伏使用jQuery从Ajax获取响应时出现延迟

function CallBGHostService(aParam) { 

var v = null; 
$.ajax({ 

    url: "http://localhost/MyService.asmx/GetBusinessGroupData", 
    type: 'POST', 
    dataType: "json", 
    data: "{'aBusinessGroupID':'" + aParam + "'}", 
    contentType: "application/json; charset=utf-8", 
    beforeSend: function (xhr) { xhr.withCredentials = true; }, 
    crossDomain: true, 
    success: function test(response) { 
     v = response.d; 
     alert(response.d); 

    }, 
    error: function test(response) { 
     v = response.d; 
     alert(response.d); 
       } 
}); 

return (v); 

} 
+0

你可以尝试改变你的'数据:'数据:{aBuisnessGroupID:aParam),'? – shaun5 2012-04-11 13:33:27

+0

@ shaun5我改变了数据,但与此服务不起作用。 – banita 2012-04-11 13:49:53

只是看你是在为你设置接收什么样的变化dataType: "text"和改变你的success功能success: function(response) {alert(response);},。您的错误功能设置不正确,应该是:error: function(XMLHttpRequest, textStatus, errorThrown){alert(errorThrown);}。您也不需要命名successerror函数。你当然不需要将它们命名为相同的东西!

+0

因为结果必须是json格式,所以我们不能更改数据类型。另外,我尝试更改成功和错误回调函数,但没有运气,仍然收到来自Web服务的迟到响应。 – banita 2012-04-13 06:39:45

+0

迟到响应是什么意思? – shaun5 2012-04-13 12:02:34