奇怪的重定向错误(POST 重定向到同一页面不更新)
Weird redirect bug (POST redirect to same page does not update)
我目前有一个 POST 请求,无论如何都会在响应中设置位置 header 以再次转到完全相同的页面。
1) 我点击登录。如您所见,位置设置为 /
,在本例中为同一页面。请求由服务器处理,然后在响应中设置通过位置字段的重定向 header.
2) 下面是对与登录页面相同地址的获取请求(不要与 /auth/local
混淆,它只接收带有登录凭据的 post)。 /
由于节点中的路由,登录时是一个不同的页面。
3) 正如您在下面看到的,预览显示了 html 对由 post 请求的响应发起的 get 请求的响应。它与实际浏览器显示的初始登录页面不同。好像没有更新浏览器?
时间线:
我是 运行 Angular 还有:
main.js
angular.module('myApp', [])
.controller('publicHomeCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.submit = function (event) {
var data = {
email: $scope.email,
password: $scope.password
};
$http.post('/auth/local', data).success(function () {
console.log('success');
});
event.preventDefault();
};
$scope.submit2 = function () {
var data = {
email: $scope.email,
password: $scope.password
};
$http.post('/auth/local', data).success(function () {
console.log('success');
});
};
}]);
登录html
<div class="container" data-ng-controller="publicHomeCtrl">
<form method="post" role="form" ng-submit="submit($event)">
<input type="email" data-ng-model="email" placeholder="your@email.here" class="form-control">
<input type="password" data-ng-model="password" placeholder="password" class="form-control">
<button type="submit" class="btn btn-success">sign in 1</button>
</form>
<button ng-click="submit2()">sign in 2</button>
</div>
我怀疑是因为我在客户端的 http.post 有一个回调,它从实际更新视图中取消了响应 header 中的重定向?
不敢相信我们错过了这个。
当从 ajax 调用中 return 编辑 302 时,浏览器不会将当前页面更改到新位置。相反,它获取新位置的内容并将 return 的内容发送到 Ajax 调用,这不是您想要的。
如果您希望浏览器页面在 ajax 调用后重定向,则您必须在客户端的 ajax 响应处理程序中自行编写代码。我建议不要使用 302。只是 return 一个普通的 200 和 return 一些 JSON 告诉客户下一步要做什么。如果客户端收到执行重定向的指令,则它可以将 window.location
自身设置为 JSON.
中新的 URL returned
我目前有一个 POST 请求,无论如何都会在响应中设置位置 header 以再次转到完全相同的页面。
1) 我点击登录。如您所见,位置设置为 /
,在本例中为同一页面。请求由服务器处理,然后在响应中设置通过位置字段的重定向 header.
2) 下面是对与登录页面相同地址的获取请求(不要与 /auth/local
混淆,它只接收带有登录凭据的 post)。 /
由于节点中的路由,登录时是一个不同的页面。
3) 正如您在下面看到的,预览显示了 html 对由 post 请求的响应发起的 get 请求的响应。它与实际浏览器显示的初始登录页面不同。好像没有更新浏览器?
时间线:
我是 运行 Angular 还有:
main.js
angular.module('myApp', [])
.controller('publicHomeCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.submit = function (event) {
var data = {
email: $scope.email,
password: $scope.password
};
$http.post('/auth/local', data).success(function () {
console.log('success');
});
event.preventDefault();
};
$scope.submit2 = function () {
var data = {
email: $scope.email,
password: $scope.password
};
$http.post('/auth/local', data).success(function () {
console.log('success');
});
};
}]);
登录html
<div class="container" data-ng-controller="publicHomeCtrl">
<form method="post" role="form" ng-submit="submit($event)">
<input type="email" data-ng-model="email" placeholder="your@email.here" class="form-control">
<input type="password" data-ng-model="password" placeholder="password" class="form-control">
<button type="submit" class="btn btn-success">sign in 1</button>
</form>
<button ng-click="submit2()">sign in 2</button>
</div>
我怀疑是因为我在客户端的 http.post 有一个回调,它从实际更新视图中取消了响应 header 中的重定向?
不敢相信我们错过了这个。
当从 ajax 调用中 return 编辑 302 时,浏览器不会将当前页面更改到新位置。相反,它获取新位置的内容并将 return 的内容发送到 Ajax 调用,这不是您想要的。
如果您希望浏览器页面在 ajax 调用后重定向,则您必须在客户端的 ajax 响应处理程序中自行编写代码。我建议不要使用 302。只是 return 一个普通的 200 和 return 一些 JSON 告诉客户下一步要做什么。如果客户端收到执行重定向的指令,则它可以将 window.location
自身设置为 JSON.