Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.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/6/codeigniter/3.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 如何在使用angularjs从登录服务获取后为特定url全局设置标题?_Javascript_Angularjs - Fatal编程技术网

Javascript 如何在使用angularjs从登录服务获取后为特定url全局设置标题?

Javascript 如何在使用angularjs从登录服务获取后为特定url全局设置标题?,javascript,angularjs,Javascript,Angularjs,我有这个登录服务: var url = GetHostUrl.cvmUrl+'/WSUser/users/login'; $http({ method : 'POST', url : url, data : $.param({ username:$scope.user_name, password:$scope.pass, domain: GetHostUrl.cvmServerUpdated }

我有这个登录服务:

var url =  GetHostUrl.cvmUrl+'/WSUser/users/login';
$http({
  method  : 'POST',
  url     : url,
  data    : $.param({
           username:$scope.user_name,
           password:$scope.pass,
           domain: GetHostUrl.cvmServerUpdated
  }), 
  headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data){
   $localstorage.authtoken = data.authtoken;
}

必须在特定URL的标题中全局传递
$localstorage.authtoken

创建用于进行HTTP调用的服务,并使用该服务获取$HTTP对象

您可以根据需要在单个位置操纵请求

您的服务代码如下所示

HTTPService={}
HTTPService.method="";
HTTPService.url="";
HTTPService.headers={}
//modify the headers over here
HTTPService.call=function(){
 return $http({
   method  : HTTPService.method,
   url     : HTTPService.url,
 )}
}
return HTTPService;
并使用此服务的调用方法在应用程序中进行调用

如果代码不清楚,我也可以发布完全相同的代码

我建议你自己创作。 所以您的登录服务可以使用HTTPService服务进行http调用,以便方法体位于一个位置


您可以根据需要覆盖特定URL的值

AngularJs中有一个拦截所有传入请求和响应的功能,这些钩子可以在发出http请求之前或收到http请求时触发。您可以将Auth头添加到请求中,请求应该成功


有关

的更多信息,我已经为所有url设置了标题属性。我必须为特定URL添加一个头的公共属性,因此通过覆盖公共服务的默认值并进行http调用来相应地更改头。您的登录服务将扩展HTTPService,并使用HTTPService.call方法进行http调用。实际上,登录服务位于其他控制器中,从登录服务获取数据后,必须将响应注入其他控制器。i、 e,app.js。在app.js中,它首先加载,然后再从登录服务获取响应。您是否使用ui-router?是的,我使用ui routerCant能够将服务响应注入到app.js(公共js文件)中的工厂。以任何其他方式继续,而不是移除uirouter@syed faizanCorrect标头可以在那里轻松设置