目前我正在试图调用jersey
REST API,我在本地上运行localhost:8080
,通过角4使用HttpClient
。
我的代码:```
ngOnInit() {
const headers = new HttpHeaders();
headers.append('Authorization', 'Basic ' + btoa('username:password'));
headers.append('Content-Type', 'application/json');
this.http.get('http://localhost:8080/mycall?myparam=myvalue', {headers: headers}).subscribe(data => {
console.log(data);
});
}
``
当我拨打这个电话时,我收到一条401
错误消息,说我是Unauthorized
。但是,当我通过邮递员运行它时,它运行得很好。我在这里做错了什么?
注意:这不是 CORS 问题,我目前正在通过 Firefox 上的扩展允许 CORS
HttpHeaders 是不可变的,所以你需要做类似的事情
ngOnInit() {
const headers = new HttpHeaders()
.append('Authorization', 'Basic ' + btoa('username:password'))
.append('Content-Type', 'application/json');
this.http.get('http://localhost:8080/mycall?myparam=myvalue', {headers: headers}).subscribe(data => {
console.log(data);
});
}
实际上,您不应该在此处为 GET 请求提供 Content-type
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句