我在 aspx 页面上编写了一个 ajax 发布请求,它将调用在其代码后面的类中编写的 web 方法。此方法返回 url 以重定向..在 ajax 调用成功函数之前一切正常,但在成功函数中我重定向到另一个页面前。
window.location.assign(data.d)
我已经通过成功功能中的警报检查了 data.d 结果,该功能显示了正确的 url,但它没有重定向到该页面..请帮助..
完整代码在这里..
这是脚本:
<script type="text/javascript">
jQuery(document).ready(function() {
$('#loginbtn').click(function() {
var userName = document.getElementById('uid').value;
var password = document.getElementById('pwd').value;
$.ajax({
type: "POST",
url: "testAjax.aspx/Authenticate",
data: JSON.stringify({ userName: userName, password: password }),
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) { window.location.assign(data.d); },
error: function(e) {
alert(e.valueOf());
}
});
//alert("dsf");
});
});
</script>
以下是网络方法:
[WebMethod]
public static string Authenticate(string userName, string password)
{
try
{
return "Home.aspx";
}
catch (Exception ex)
{
return string.Empty;
}
}
请注意:如果我取消注释警报(“dsf”),一切正常,它会成功重定向到 Home.aspx ..但没有这个警报它不会重定向。