使用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);
}
答
只是看你是在为你设置接收什么样的变化dataType: "text"
和改变你的success
功能success: function(response) {alert(response);},
。您的错误功能设置不正确,应该是:error: function(XMLHttpRequest, textStatus, errorThrown){alert(errorThrown);}
。您也不需要命名success
和error
函数。你当然不需要将它们命名为相同的东西!
你可以尝试改变你的'数据:'数据:{aBuisnessGroupID:aParam),'? – shaun5 2012-04-11 13:33:27
@ shaun5我改变了数据,但与此服务不起作用。 – banita 2012-04-11 13:49:53