可能重复:
如何从异步调用返回响应?
我正在使用 Jquery Ajax 调用服务来更新值。
function ChangePurpose(Vid, PurId) {
var Success = false;
$.ajax({
type: "POST",
url: "CHService.asmx/SavePurpose",
dataType: "text",
data: JSON.stringify({ Vid: Vid, PurpId: PurId }),
contentType: "application/json; charset=utf-8",
success: function (data) {
Success = true;//doesn't go here
},
error: function (textStatus, errorThrown) {
Success = false;//doesn't go here
}
});
//done after here
return Success;
}
和服务:
[WebMethod]
public string SavePurpose(int Vid, int PurpId)
{
try
{
CHData.UpdatePurpose(Vid, PurpId);
//List<IDName> abc = new List<IDName>();
//abc.Add(new IDName { Name=1, value="Success" });
return "Success";
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
正在从 AJAX 成功调用该服务。价值也在改变。但是在服务之后,success:或error:函数没有被调用,在这种情况下,success 应该被调用但它不起作用。
我使用了firebug,发现成功或错误功能被跳过并直接转到return Success;
似乎找不到代码有什么问题。
更新:
添加async: false
修复了问题