发布于 2017-07-12 08:16:52 | 115 次阅读 | 评论: 0 | 来源: 网友投递
Ajax
AJAX即“Asynchronous Javascript + XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术。
$.ajax({
url: '/oauth/respond',
type: 'post',
data: data,
complete: function(jqXHR){
console.log(jqXHR.status);
},
error: function (xhr) {
console.log(xhr.status);
}
});
在ajax的complete()与error()回调函数中得到的状态码都是404,而不是302。
这是为什么呢?
在stackoverflow上找到了
You can't handle redirects with XHR callbacks because the browser takes care of them automatically. You will only get back what at the redirected location.
如何解决?
方法一
继续用ajax,修改服务器端代码,将原来的302响应改为json响应,比如下面的ASP.NET MVC示例代码:
return Json(new { status = 302, location = "/oauth/respond" });
$.ajax({
url: '/oauth/respond',
type: 'post',
data: data,
dataType: 'json',
success: function (data) {
if (data.status == 302) {
location.href = data.location;
}
}
});
方法二
不用ajax,改用form。
<form method="post" action="/oauth/respond">
</form>