我需要在用户登录后为每个后续请求设置一些授权标头.
要为特定请求设置标头,
import {Headers} from 'angular2/http';
var headers = new Headers();
headers.append(headerName, value);
// HTTP POST using these headers
this.http.post(url, data, {
headers: headers
})
// do something with the response
Run Code Online (Sandbox Code Playgroud)
但是以这种方式为每个请求手动设置请求标头是不可行的.
如何在用户登录后设置标头集,并在注销时删除这些标头?
我尝试使用新的Angular 4.3拦截器为所有请求设置authorithation标头.但是,它不起作用.我将断点设置为拦截器intercept方法,浏览器没有命中它,所以它似乎angular只是忽略了我的拦截器.请帮助我,提前谢谢.
user.service.ts:
import {Injectable} from '@angular/core';
import 'rxjs/add/operator/map';
import {Observable} from "rxjs";
import {Http} from "@angular/http";
@Injectable()
export class UserService {
constructor(public http: Http) {
}
public getContacts(): Observable<any[]> {
return this.http.get('/users/contacts').map(contacts => contacts.json());
}
}
Run Code Online (Sandbox Code Playgroud)
token.interceptor.ts
import {Injectable} from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor
} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {AuthService} from "./shared/auth.service";
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(public auth: AuthService) {
}
intercept(request: HttpRequest<any>, next: …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现ConfigService以检索项目中正确环境的正确配置.我目前正在遇到循环依赖
(index):28 Error: (SystemJS) Provider parse errors:
Cannot instantiate cyclic dependency! Http: in NgModule AppModule
Error: Provider parse errors:
Run Code Online (Sandbox Code Playgroud)
在我看来,我已经探索过代码并且存在问题:
CustomHttp
constructor(backend: XHRBackend, options: RequestOptions, public spinnerService: SpinnerService, public exceptionService: ExceptionService, public configService: ConfigService)
Run Code Online (Sandbox Code Playgroud)
ExceptionService
constructor(private _notificationService: NotificationService, private _spinnerService: SpinnerService, private _configService: ConfigService, private _router: Router)
Run Code Online (Sandbox Code Playgroud)
的ConfigService
constructor(private http: Http) {}
Run Code Online (Sandbox Code Playgroud)
如您所见,我在此图中说明了一个循环依赖(没有任何好的约定):
我现在的问题是,如何修复它?我听说过,Injector但我不确定我是否真的可以在这种情况下使用它.
提前感谢您的回答.
我正在尝试非常简单的角度4 http请求.当我检查chrome开发人员工具时,我看不到http标头.
const headers: Headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'Bearer: 123213');
this.http.post('https://127.0.0.1:502', JSON.stringify(token), {headers: headers}).subscribe();
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
我正在使用 auth0 中的 angular2-jwt 包。一切工作正常,但现在我想知道如何重定向当前位于某个路径上的用户,该路径由我的身份验证守卫在其他地方保护。现在,当用户尝试访问受保护的路径时,它会进行重定向。
我知道我可以隐藏组件中的对象,但是我必须在每个受保护的组件中执行此操作,这还不清楚。
这是我的路线:
const appRoutes: Routes = [
{path: 'login', component: LoginComponent},
{path: 'register', component: RegisterComponent},
{path: '', component: HomeComponent},
{path: 'user/edit', component: EditProfileComponent, canActivate: [AuthGuard]},
];
Run Code Online (Sandbox Code Playgroud)
这是我的警卫服务:
...
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private auth: AuthService, private router: Router) {
}
canActivate() {
if (this.auth.loggedIn()) {
return true;
}
this.router.navigate(['/']);
return false;
}
}
Run Code Online (Sandbox Code Playgroud) 在角度4中我使用了一个代码片段;
let h = [{Content: 'application/json'}];
this.http.post(server, this.userDataObj, {headers: h}).....
Run Code Online (Sandbox Code Playgroud)
所以基本上我想在我的ajax调用中添加一个标题.
现在我经常遇到错误
ERROR TypeError: values.join is not a function
我检查了错误位置,我发现在http.es5.js文件中有一个代码,如;
req.headers.forEach(function (name, values) {
return xhr.setRequestHeader(name, values.join(','));
});
Run Code Online (Sandbox Code Playgroud)
现在我console.log(req.headers)在片段上方添加了一个,我得到了;
[Object]
0:Object
Content: 'application/json'
Run Code Online (Sandbox Code Playgroud)
现在据我所知,函数里面的forEach循环在JS中的数组中取第二个参数(这是http.es5.js中的值)是元素的索引.我测试了通过console.log(values),我得到了结果0.所以自然值values.join不是一个函数,它永远不会是一个整数的函数.
我试过了 ;
var headers = new Headers();
headers.append("Content", 'application/json');
this.http.post(server, this.userDataObj, {headers: headers}).subscribe(.....
Run Code Online (Sandbox Code Playgroud)
这也给了我同样的错误.
试过这个;
var h = new Headers();
h.append("kkk", 'aaa');
let options = new RequestOptions({ headers: h });
this.http.post(server, this.userDataObj, options).subscribe(.....
Run Code Online (Sandbox Code Playgroud)
还是一样的错误.
这是一个错误吗?或者我有任何错误吗?任何想法都对我很有帮助.
PS:我是Angular4的新手.
我开始使用 Angular HttpHeaders 但无法理解其构造函数的参数之一,即[name: string]: string,这是文档中的构造函数:
constructor(headers?: string | { **[name: string]: string** | string[]; })
Run Code Online (Sandbox Code Playgroud)
谁能解释该对象的类型或它所指的是什么?