【发布时间】:2016-01-14 07:11:50
【问题描述】:
我已阅读其他关于移动 safari 如何缓存 POST 请求的问题的 SO 条目。我已经在我的 express 应用程序上实施了我“认为”的修复程序,但是当我从 Angular 客户端发布到我的应用程序(托管在 Heroku 上)时,我从 Heroku 获得状态 204,并且没有返回任何内容。
这是在客户端执行 POST 的控制器函数:
$scope.signUp = function(user){
$scope.passwordsmatch = true;
$scope.waitingToSignUp = true;
$scope.noSignUpError = true;
if(user.password != user.password2) {
$scope.passwordsmatch = false;
return;
}
if($scope.signupForm.$valid) {
$http({
url: "https://frozen-peak-5921.herokuapp.com/api/users",
method: "POST",
data: user,
headers: {'Content-Type': 'application/json'}})
.success(function(obj){
$scope.resultAction(obj);
})
.error (function(err){
$scope.resultAction(err);
});
}
else{
$scope.showValidation = true;
}
};
这是 Express 中的 POST 路由:
router.route('/users')
.post(function(req, res) {
var user = new User();
user.username = req.body.username;
user.password = req.body.password;
user.save(function(err){
if (err) {
res.status(500);
res.json({
status: 500,
error: err
});
}
else {
res.json({
status: 200,
user: user
});
}
});
});
这是我的标头设置我的 Express 中间件的方式:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With,
Content-Type, Accept");
res.header("Content-Type", "application/json");
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
});
这是我在 Heroku 日志中看到的内容
at=info method=OPTIONS path="/api/users" host=<myhost removed on purpose>.herokuapp.com
request_id=dd16af0c-5a77-4c1e-9f88-73e97e6de89d fwd="70.197.166.161"
dyno=web.1 connect=1ms service=5ms status=204 bytes=297
此 POST 在其他浏览器上完美运行,只是不是移动 safari?还有什么我需要做的吗?
【问题讨论】:
标签: angularjs node.js express heroku safari