Angular 7 未按要求发送正确的 header
Angular 7 not sending correct header on request
我无法在 angular 7 中发送正确的标题!
我在后端使用护照:
"app.get('/api/users', passport.authenticate('jwt', {session: false}), (req, res, next) => {... bla bla bla...}."
当我尝试从邮递员向我的路径/api/用户发送一个有效令牌时,一切正常,但是当我从angular发送时却不行。 (*Angular 不显示错误!)
假设 'Bearer validToken' 是一个有效的标记
通过这种方式,我发送 headers:
getAll() {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Bearer validToke.'
});
};
return this.http.get<user[]>(`http://localhost:3000/api/users`, httpOptions);
}
我可以从 angular 进行身份验证并获取 Bearer 令牌,一切正常,如果我删除 passport.authenticate ('jwt', {session: 错误})
感谢阅读!
一切对我来说似乎都是正确的 - 但我对你的代码做了一些小改动
不绑定HttpOptions
直接绑定请求中的HttpHeaders
检查下面的代码
getAll() {
const httpHeaders = new HttpHeaders ({
'Content-Type': 'application/json',
'Authorization': 'Bearer validToke.'
});
return this.http.get<user[]>(`http://localhost:3000/api/users`, { headers: httpHeaders });
}
现在这将为请求添加您的 headers - 如果您使用从不同域获取数据 - 尝试在您的服务器上添加 cors - 浏览器将 pre-flight 请求作为 HttpOptions
然后在你的情况下将其绑定为 HttpGet
希望它有效 - 编码愉快!!
我能够通过创建一个拦截器来解决这个问题:
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable } from 'rxjs';
import { AuthenticationService } from '../_services/authentication.service';
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
constructor(private authenticationService: AuthenticationService) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// add authorization header with jwt token if available
let currentUser = this.authenticationService.currentUserValue;
if (currentUser && currentUser.token) {
request = request.clone({
setHeaders: {
Authorization: `${currentUser.token}`
}
});
}
return next.handle(request);
}
}
谢谢
我无法在 angular 7 中发送正确的标题! 我在后端使用护照:
"app.get('/api/users', passport.authenticate('jwt', {session: false}), (req, res, next) => {... bla bla bla...}."
当我尝试从邮递员向我的路径/api/用户发送一个有效令牌时,一切正常,但是当我从angular发送时却不行。 (*Angular 不显示错误!) 假设 'Bearer validToken' 是一个有效的标记 通过这种方式,我发送 headers:
getAll() {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Bearer validToke.'
});
};
return this.http.get<user[]>(`http://localhost:3000/api/users`, httpOptions);
}
我可以从 angular 进行身份验证并获取 Bearer 令牌,一切正常,如果我删除 passport.authenticate ('jwt', {session: 错误})
感谢阅读!
一切对我来说似乎都是正确的 - 但我对你的代码做了一些小改动
不绑定HttpOptions
直接绑定请求中的HttpHeaders
检查下面的代码
getAll() {
const httpHeaders = new HttpHeaders ({
'Content-Type': 'application/json',
'Authorization': 'Bearer validToke.'
});
return this.http.get<user[]>(`http://localhost:3000/api/users`, { headers: httpHeaders });
}
现在这将为请求添加您的 headers - 如果您使用从不同域获取数据 - 尝试在您的服务器上添加 cors - 浏览器将 pre-flight 请求作为 HttpOptions
然后在你的情况下将其绑定为 HttpGet
希望它有效 - 编码愉快!!
我能够通过创建一个拦截器来解决这个问题:
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable } from 'rxjs';
import { AuthenticationService } from '../_services/authentication.service';
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
constructor(private authenticationService: AuthenticationService) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// add authorization header with jwt token if available
let currentUser = this.authenticationService.currentUserValue;
if (currentUser && currentUser.token) {
request = request.clone({
setHeaders: {
Authorization: `${currentUser.token}`
}
});
}
return next.handle(request);
}
}
谢谢