Javascript $httpProvider侦听器无法设置未定义的属性“x”
下面是我想在每个http请求中添加身份验证令牌和idx的交易。 当我尝试时,我得到一个TypeError:无法设置未定义的属性'site' 这是拦截器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
$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。因此,我必须手动检查数据,然后进行设置。查看带有相同错误的帖子:谢谢,但问题不一样。