【发布时间】:2012-12-21 00:09:15
【问题描述】:
当我使用 jQuery ajax 向 Web 服务发出请求时,我发现了一个奇妙的错误:
var AjaxResult;
login = function () {
AjaxResult = "";
$.ajax({
type: "POST",
url: KnutBase.getServiceUrl() + "ServiceInterface/HmsPlannerWebService.asmx/AuthenticateLogin",
data: { username: this.username, password: this.password },
dataType: 'jsonp',
success: function (response) {
//the response value is 'success'
AjaxResult = response;
},
error: function (data, status, e) {
alert("error:" + e);
}
});
//alert(AjaxResult);
//if i do not alert a message here, this function will return a null value
//however, i do alert here, then i get a 'success' value.
return AjaxResult;
}
怎么会这样。 我很困惑为什么 AjaxResult 在 ajax 方法设置响应值之前返回它的值。
【问题讨论】:
-
我不知道你从哪里得到了那个 sn-p,但它只是有问题。你是对的,它总是返回空字符串,这不会随着异步回调而改变。
-
你是否在全局ajax默认选项中设置了
{async:false}? -
method:"POST"和dataType:"jsonp"不能一起工作; JSONP 脚本始终是 GET 请求。 -
贝尔吉,感谢您的帮助。
-
@JackHe:它有效,只是不适用于 JSONP。而且它会让浏览器冻结直到它返回,这就是为什么不关心用户的人应该使用它的原因。
标签: javascript jquery ajax jsonp