我試圖爲$ http服務添加一個默認頭,該值是用戶認證後生成的訪問令牌。
.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
$httpProvider.interceptors.push('httpRequestInterceptor');
}])
.factory('httpRequestInterceptor', function() {
return {
request: function (config) {
config.headers = {
'Authorization': sessionStorage.token}
return config;
}
};
})
但是Angular不接受標題:A ccess-Control-Request-He... authorization
。 我嘗試$http.defaults.headers.common.Authorization = 'Basic YmVlcDpib29w'
和很多其他但沒有任何作品。 我用Rest Console測試了API,她接受了頭文件。
的問題來自於我的應用程序...
響應
- Access-Control-Allow-Head... Accept,Origin,Authorization,Content-Type,X-Requested-With
- Access-Control-Allow-Meth... GET, POST, PUT, DELETE
Access-Control-Allow-Orig... *
- Access-Control-Expose-Hea... Authorization Cache-Control no-store,
no-cache, must-revalidate, post-check=0, pre-check=0
請求
- Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
- Accept-Encoding gzip, deflate
- Accept-Language en-us,en;q=0.5
- **Access-Control-Request-He... authorization**
- Access-Control-Request-Me... GET
- Connection keep-alive
- Host api.mysite.com
- Origin http://xxxxx.com:8080