使用Angular 5 HttpClient获取数据 [英] Fetching data using Angular 5 HttpClient
问题描述
我正在尝试使用Angular提供的HttpClient从其余端点加载数据.有问题的端点使用令牌来授权用户.
I am trying to load data from a rest endpoint using the HttpClient provided by Angular. The endpoint in question uses a token to authorize the user.
getData() {
if (this.token) {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Bearer' + ' ' + this.token
})
};
this.http.get('https://some url', httpOptions).subscribe((res) => {
console.log(res);
}, (error) => {
console.log(error);
});
} else {
this.empty_token = true;
}
}
尝试执行此操作时出现以下错误: 对预检的响应具有无效的HTTP状态代码401.
I get the following error while trying to do that: Response for preflight has invalid HTTP status code 401.
但是,相同的代码适用于在"http" 上使用的某些演示网址,而不适用于"https" .
However, the same code works for a some demo url that works on "http" and not for "https".
我尝试通过邮递员或任何其他RestClient发出请求来使用相同的内容,并且效果很好.
I tried using the same by making a request using Postman or any other RestClient for that matter and it works fine.
推荐答案
您应该添加
"proxies": [
{
"path": "/v1",
"proxyUrl": "'https://some url'"
}
到您的项目.
您可以查看此链接 https://github.com/mgechev/angular-seed/wiki/Add-a-Proxy-to-browsersync
这篇关于使用Angular 5 HttpClient获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!