0

我的代码有奇怪的行为。我正在使用 Satellizer 对用户进行身份验证,当我执行此代码时用户未通过身份验证:

$http.get('http://eune.api.pvp.net/api/lol/eune/v1.4/summoner/by-name/somename?api_key=XXXXXXXXXXXXXXXXX')
                    .success(function (data) {
                        console.log(data);
                    });

我的请求没问题,我得到了数据

标题:

Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Host:eune.api.pvp.net
Origin:http://localhost:9000
Referer:http://localhost:9000/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36

但是当我对用户进行身份验证并尝试执行相同的请求时,我得到:

XMLHttpRequest cannot load http://eune.api.pvp.net/api/lol/eune/v1.4/summoner/by-name/somename?api_key=XXXXXXXXXXXX. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access. The response had HTTP status code 401.

此请求的标头如下所示:

Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
Access-Control-Request-Headers:accept, authorization
Access-Control-Request-Method:GET
Connection:keep-alive
Host:eune.api.pvp.net
Origin:http://localhost:9000
Referer:http://localhost:9000/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36

我的 app.config

.config(function ($urlRouterProvider, $stateProvider, $httpProvider, $authProvider, API_URL) {

            $urlRouterProvider.otherwise('/');
            ... some routes ...
                    $authProvider.loginUrl = API_URL + 'login';
                    $authProvider.signupUrl = API_URL + 'register';
                    $authProvider.google({
                        clientId: 'secret',
                        url: API_URL + 'auth/google'
                    });
                    $authProvider.facebook({
                        clientId: 'secret',
                        url: API_URL + 'auth/facebook'
                    });
//                    $httpProvider.interceptors.push('authInterceptor');
        })

那么我应该如何解决呢?我想那些带有 Access-Control 的标头是原因,但是我应该如何处理呢?

4

3 回答 3

4

您可以尝试将以下内容放入satellizer config

$authProvider.httpInterceptor = false;
于 2015-05-31T20:42:36.440 回答
1

在配置块中添加skipAuthorization属性可能会有所帮助:

$http.get('http://eune.api.pvp.net/api/lol/eune/v1.4/summoner/by-name/somename?api_key=XXXXXXXXXXXXXXXXX', {
    skipAuthorization: true
  })
  .success(function (data) {
    console.log(data);
  });

我通常根据偏好使用配置块。这就是它的样子。//配置块方法:

$http({
      method: 'GET',
      url: 'http://eune.api.pvp.net/api/lol/eune/v1.4/summoner/by-name/somename?api_key=XXXXXXXXXXXXXXXXX',
      skipAuthorization: true
    });  

祝你好运。

于 2015-10-18T06:50:50.140 回答
0

好的,我想通了。正如我所认为的那样,Satellizer 注册了新的拦截器,它添加了一些标题,这就是它不起作用的原因。这是卫星器代码:

.config(['$httpProvider', 'satellizer.config', function($httpProvider, config) {
      $httpProvider.interceptors.push(['$q', function($q) {
        var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
        return {
          request: function(httpConfig) {
            var token = localStorage.getItem(tokenName);
            if (token && config.httpInterceptor) {
              token = config.authHeader === 'Authorization' ? 'Bearer ' + token : token;
              httpConfig.headers[config.authHeader] = token;
            }
            return httpConfig;
          },
          responseError: function(response) {
            return $q.reject(response);
          }
        };
      }]);
    }]);

我通过改变一条车道来处理它:

if (token && config.httpInterceptor && httpConfig.rawReq !== true) {

我传入了我的 httpConfig 选项rawReq: true ,但这并不好。是否有可能禁用特定的拦截器?

于 2015-02-03T13:15:54.060 回答