Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript $httpProvider侦听器无法设置未定义的属性“x”_Javascript_Angularjs_Http_Undefined_Interceptor - Fatal编程技术网

Javascript $httpProvider侦听器无法设置未定义的属性“x”

Javascript $httpProvider侦听器无法设置未定义的属性“x”,javascript,angularjs,http,undefined,interceptor,Javascript,Angularjs,Http,Undefined,Interceptor,下面是我想在每个http请求中添加身份验证令牌和idx的交易。 当我尝试时,我得到一个TypeError:无法设置未定义的属性'site' 这是拦截器 $httpProvider.interceptors.push(function($q, $cookies,$location) { return { 'request': function(config) { config.data.auth ='hasAuth'; config.data.token

下面是我想在每个http请求中添加身份验证令牌和idx的交易。 当我尝试时,我得到一个TypeError:无法设置未定义的属性'site' 这是拦截器

$httpProvider.interceptors.push(function($q, $cookies,$location) {
  return {
    'request': function(config) {   
      config.data.auth ='hasAuth';
      config.data.token = $cookies.token;
      config.data.idx = $cookies.idx;
      console.log(config);

      return config;
    }

  };
});
下面是$http请求

app.controller('coolCtrl', function($scope, $http, makeUrl, $cookies){
    var info={};
    info = $cookies.eidx;

    var test = $http.post("https://example.com/uri/list", info )
    .success(function (data) {
        $scope.data = data
    });

});
错误呢 TypeError:无法设置未定义的属性“auth”

我觉得我被所有的承诺都搅在一起了。 提前谢谢
-詹姆斯

经过多次调试,我终于找到了答案

$httpProvider.interceptors.push(function($q, $cookies,$location) {
return {
    'request': function(config) { 
      if (typeof config.data !== undefined){
        config.data.auth ='hasAuth';
        config.data.token = $cookies.token;
        config.data.idx = $cookies.idx;
        console.log(config);
      }
      return config;
    }

  };
});

angular在发出任何请求(包括无数据get请求)时试图设置config.data.auth。因此,我必须手动检查数据,然后进行设置。

查看带有相同错误的帖子:谢谢,但问题不一样。