节点js / Angular js - 注意:显示临时标头

时间:2014-02-07 14:42:42

标签: javascript node.js angularjs http-headers xmlhttprequest

这是我的 Angular js 片段代码:

$http({
                method:'POST',
                withCredential:true,
                url:$scope.config.app_ws+'auth/signup',
                data:{user:$scope.auth}
            }).success(function(status, response){

                console.log(response);
            }).error(function(status, response){
                alert(response+'Bummer :( , an error occured plese retry later. ');
            });

这是我的 Node.js 片段后端:

 var allow_cross_domain= function(req, res, next) {
      res.header('X-Powered-By', 'hey.heyssssssss.org');

      var oneof = false;
      if(req.headers.origin) {
        res.header('Access-Control-Allow-Origin', req.headers.origin);
        oneof = true;
      }
      if(req.headers['access-control-request-method']) {
        res.header('Access-Control-Allow-Methods', req.headers['access-control-request-method']);
        oneof = true;
      }
      if(req.headers['access-control-request-headers']) {
        res.header('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
        oneof = true;
      }
      if(oneof) {
        res.header('Access-Control-Max-Age', 60 * 60 * 24 * 365);
      }
    // intercept OPTIONS method
    if (oneof && req.method == 'OPTIONS') {
      res.send(200);
    } else {
      next();
    }
    }

    app.use(allow_cross_domain);

    app.post('/auth/signup', function (req, res) { res.send('wtff'); });

我只是从Angular到Node调用POST localhost:3000 / auth / signup,但我在chrome console中得到**CAUTION : Provisional headers are shown.**

  

Chrome(注意):

enter image description here

  

Firefox(没有响应约30/60秒然后警报()出现:/):

enter image description here

这可能是什么?

如果我使用GET,一切都还可以,只是使用POST,我会遇到麻烦,怎么可能?

2 个答案:

答案 0 :(得分:10)

personaly我在角度中使用这种“重置”方法:

app.config(['$httpProvider', function ($httpProvider) {
  //Reset headers to avoid OPTIONS request (aka preflight)
  $httpProvider.defaults.headers.common = {};
  $httpProvider.defaults.headers.post = {};
  $httpProvider.defaults.headers.put = {};
  $httpProvider.defaults.headers.patch = {};
}]);

答案 1 :(得分:2)

这是在2014年3月28日修复的 所以在下一版的crome(v35)中它会好的

chromium community