Ionic - $http.get() 未发送授权 Header

Ionic - $http.get() not sending Authorization Header

Ionic 应用未发送授权 Header。 Header 值根本不存在,即使授权 Header 列在允许的 Header 中。 我正在使用 NodeJS Express 和 MongoDB 作为后端 这是我的 CORS Headers

    <code>
res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods',
      'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Expose-Headers',
      'X-Authorization-Token, Authorization');
    res.header('Access-Control-Allow-Headers',
      'Origin, X-Requested-With, Content-Type, Accept, Authorization'
    );

   And Here Is My AuthInterceptor


    return {
      request: function(config) {
        var token = authToken.getToken();

        if (token) {
          config.headers.Authorization = 'Bearer ' + token;

        }
        return config;
      },
      response: function(response) {
        return response;
      }
    };

My $http

        return $http(options).success(function(response) {
          self.currUser = response.profile;
          if (_.contains(response.profile.roles, 'admin') ||
            !((_.contains(response.profile.roles, 'lecturer')) || (_.contains(
              response.profile.roles, 'evaluator')))) {
            $state.go('admin');
          }
        })
</code> 

您可以在调用 $http 之前通过身份验证 header。我以这种方式使用过,这对我有用 API auth,

$http.defaults.headers.get = { 'x-username' User.email,
                           'x-token':authToken.getToken(), 
                           'Content-Type':'application/json'};

$http.get(URL+'/api).then(function(res){console.log('success')});

希望对您有所帮助。

我遇到了同样的问题。我想出解决它的唯一方法是添加:

"proxies": [
{
  "path": "/<your local url>",
  "proxyUrl": "http://<remote_url>"
}
],

到我的 ionic.project 文件。