angular单$ http请求,节点显示两个请求

时间:2014-11-04 19:28:56

标签: javascript angularjs node.js

以角度形式发出的单个请求在节点上显示两个请求,一个请求x-auth-token为未定义,另一个请求为令牌。

我最终深入研究了节点,因为我的角度前端出现了错误(附截图)。

frontend error backenderror

Angular Code:

    var self = this;
    var $http = angular.injector(["ng"]).get("$http");
    var authToken = localStorage.getItem("authToken") ? localStorage.getItem("authToken") : Config.authToken;

    $http.defaults.headers.common["X-Auth-Token"] = authToken;

    $http.get(Config.url)
    .success(function(data, status, headers, config) {
        self.onResult(data);
    })
    .error(function(data, status, headers, config) {
        console.log(data); //this is empty
        console.log(status); //stauts is 0
        console.log(headers); 
        console.log(config);
        self.onFault(data);
    });

Node.js

    var self = this;

    app.use(function(request, response, next){
        response.set("X-Powered-By", "FIC");
        response.header("Access-Control-Allow-Origin", "*");
        response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Auth-Token");
        request.id = self.id++;
        console.log(request.id, request.url, request.headers['x-auth-token']); 
        next();
    });

节点控制台输出

0 '/attendees?appId=ag.casa.fico' undefined
1 '/attendees?appId=ag.casa.fico' 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbEFkZHJlc3MiOiJjcmlzdGlhbi5yYWN1QGJpcm91bGRlY3JlZGl0LnJvIiwicGFzc3dvcmQiOiIxMDU3In0.X7rztUCDRngXLV6ZABvv7hqwzvWEJi1Nulxg4KWL-58'

1 个答案:

答案 0 :(得分:0)

您是否尝试将所有设置放入对象中,然后将其传递给$ http请求

var req = {
    method: 'POST',
    url: 'http://example.com',
    headers: {
       'Content-Type': undefined
    },
        data: { test: 'test' },
}

$http(req).success(function(){...}).error(function(){...});

请参阅此处https://docs.angularjs.org/api/ng/service/ $ http#setting-http-headers