2014-11-04 151 views
0

角度中的單個請求在節點上顯示兩個請求,一個使用x-auth-token作爲未定義,另一個使用令牌。

由於在我的角度前端(附加屏幕截圖)中出現以下錯誤,最終我深入瞭解了節點。

frontend errorbackenderror

角代碼:

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' 
+2

你能也記錄HTTP方法?如果這是一個不同的主機,瀏覽器很可能會在實際的GET – David 2014-11-04 19:32:58

+2

之前發出一個OPTIONS請求,請參閱預衝的請求https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests – David 2014-11-04 19:34:18

+0

是的它是OPTIONS - >'0'/attendees?appId=ag.cas.fic''OPTIONS'undefined'但是問題是爲什麼角度最後是狀態0的'error'函數調用,我應該記錄'response'輸出的結束? – user2727195 2014-11-04 19:36:03

回答

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設置#-HTTP報頭