HTTP 调用 - req.url 未定义



我想从API链接获取数据。API 链接和 API 密钥是正确的。当我尝试使用邮递员时,它会返回结果。当我使用 http 调用运行应用程序时,它会给出此错误:

"未捕获(承诺(:类型错误:req.url 未定义 HttpXsrfInterceptor.prototype.intercept...

有人可以告诉我有什么问题吗?

这是我的代码。

应用模块.ts

import { HttpClientModule, HttpClient } from '@angular/common/http';
@NgModule({
imports: [ 
HttpModule ]
})

首页

import { HttpHeaders, HttpClient } from '@angular/common/http';
export class A{
apiUrl = "yyy-yyy-yyy";
constructor(private http: HttpClient){
this.getData();
}
getData(){
let headers =  { headers: new  HttpHeaders({ 'Accept':  'application/json',
'user-key': 'xxx-xxx'})}; 
return this.http.get(this.apiUrl, headers).subscribe(res=>
console.log('RES: ', res));
}
}

错误截图;

在此处输入图像描述

首先,您希望拥有这样的服务:

服务.ts

constructor(private http: Http
) { }
public mygetdata(): Observable<Data[]> {
let headers = new Headers();
headers.append('user-key': 'xxx-xxx');
return this.http.get(this.apiUrl), {
headers: headers
})
.map((response: Response) => {
let res = response.json();
if (res.StatusCode === 1) {
} else {
return res.StatusDescription.map(data=> {
return new Data(data);
});
}
})
}

组件.ts

public data : Data[];
getdata() {
this.service.mygetdata().subscribe(
data => {
this.data = data;
}
);
}

最新更新