POST Node / Express无法在Safari移动设备上运行

时间:2015-10-15 17:43:36

标签: angularjs node.js express heroku safari

我已阅读其他有关移动版Safari将如何缓存POST请求的问题的SO条目。我已经实现了我“认为”是我的快速应用程序上的修复程序,但是当我从我的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
            });

        }
    });
});

以下是我的Headers设置我的Express Middleware的方法:

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在其他浏览器中运行得很完美,只是不是移动游戏?我还需要做些什么吗?

2 个答案:

答案 0 :(得分:0)

你有没有机会尝试设置Scanner sc=new Scanner(System.in); String input = sc.nextLine(); if(input.isEmpty()) { System.out.println("Scanner can not be empty"); }

cache: false

答案 1 :(得分:0)

我设法解决了这个问题。一旦我在OSX上的Safari中看到问题也发生了,我开始研究不同浏览器如何处理POST请求的其他差异(在移动Safari之前提到的Cache问题之外)。事实证明,这是一个CORS配置问题。这篇文章帮我解决了CORS request not working in Safari

我正在使用cors npm模块,最终这是在Safari和所有其他浏览器中运行的配置:

var whitelist = ['http://myclient.com', 'http://www.myclient.com'];

var corsOptions = {
origin: function(origin, callback){
    var originIsWhitelisted = whitelist.indexOf(origin) !== -1;
    callback(null, originIsWhitelisted);
},
methods:["GET", "PUT", "POST", "DELETE"],
allowedHeaders:["Origin", "X-Requested-With", "Content-Type", "Accept"],
maxAge:-1
};

现在我将maxAge设置为-1,这会禁用POST请求的缓存。没有它,一旦CORS问题得到排序,我就有可能在移动Safari中遇到缓存问题,但最终,CORS配置似乎对我有用。