如何在 angular 6 中添加 JWT
how to add JWT in angular 6
我已经添加了下面给出的授权密钥
postrequest(callurl, input) {
const headers: any = new Headers;
headers.append('Content-type', 'application/json;charset=utf-8');
headers.append('Authorization', 'Bearer
'+localStorage.getItem("AuthTocken"));
return this._http.post(callurl, input, headers)
.pipe(
map((res: any) => {
return res;
}),
retry(2),
catchError(err => {
})
);
}
我没有发现任何错误,但是 Authorisation 在 header
中找不到
angular 6
service used from @angular/common/http
URL working fine in postman
我得到如下错误
http.service.ts:84 Backend returned code 0, body was: [object ProgressEvent] body was: {"headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"Unknown Error","url":null,"ok":false,"name":"HttpErrorResponse","message":"Http failure response for (unknown url): 0 Unknown Error","error":{"isTrusted":true}}
(anonymous) @ http.service.ts:84
push../node_modules/rxjs/_esm5/internal/operators/catchError.js.CatchSubscriber.error @ catchError.js:34
回应Headers
enter image description here
它需要稍微不同的设置。
编辑:为简洁起见
this.httpClient.post(URLs, { headers: new HttpHeaders({ 'Authorization': localStorage.getItem("token") }); })
顺便说一句,我发现你拼错了 Authorization。因此,它不会被识别为标准 header。
您必须使用 Bearer
而不是 Token
headers.append('Authorisation', 'Bearer '+'localStorage.getItem("AuthTocken"));
header object 是一个选项。
你必须以下面的形式 header 作为参数传递
this.http.post(url, data, {
headers: headers
})
我已经添加了下面给出的授权密钥
postrequest(callurl, input) {
const headers: any = new Headers;
headers.append('Content-type', 'application/json;charset=utf-8');
headers.append('Authorization', 'Bearer
'+localStorage.getItem("AuthTocken"));
return this._http.post(callurl, input, headers)
.pipe(
map((res: any) => {
return res;
}),
retry(2),
catchError(err => {
})
);
}
我没有发现任何错误,但是 Authorisation 在 header
中找不到angular 6
service used from @angular/common/http
URL working fine in postman
我得到如下错误
http.service.ts:84 Backend returned code 0, body was: [object ProgressEvent] body was: {"headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"Unknown Error","url":null,"ok":false,"name":"HttpErrorResponse","message":"Http failure response for (unknown url): 0 Unknown Error","error":{"isTrusted":true}} (anonymous) @ http.service.ts:84 push../node_modules/rxjs/_esm5/internal/operators/catchError.js.CatchSubscriber.error @ catchError.js:34
回应Headers enter image description here
它需要稍微不同的设置。
编辑:为简洁起见
this.httpClient.post(URLs, { headers: new HttpHeaders({ 'Authorization': localStorage.getItem("token") }); })
顺便说一句,我发现你拼错了 Authorization。因此,它不会被识别为标准 header。
您必须使用 Bearer
而不是 Token
headers.append('Authorisation', 'Bearer '+'localStorage.getItem("AuthTocken"));
header object 是一个选项。 你必须以下面的形式 header 作为参数传递
this.http.post(url, data, {
headers: headers
})