使用jQuery从ajax调用时,从Web服务获得响应的延迟

时间:2012-04-11 12:00:27

标签: .net web-services jquery

我正在使用jQuery调用Web服务(c#)方法。在从Web服务获取结果后,我必须以html格式绑定数据。但问题是,在运行代码时,我从Web服务到响应迟到那个时候我的函数返回null而没有设置表单的字段。 下面是我的代码。在这里,我得到v

的空值
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);

}

1 个答案:

答案 0 :(得分:0)

只需在设置中查看您收到的更改dataType: "text",然后将success功能更改为success: function(response) {alert(response);},。您的错误功能设置不正确应该是:error: function(XMLHttpRequest, textStatus, errorThrown){alert(errorThrown);}。此外,您无需为successerror功能命名。你当然不需要为它们命名相同的东西!