angular - Angular 没有重载匹配这个调用
问题描述
我正在尝试使用 Angular从flightaware获取数据 Json api。并且所有来自 flightaware 的请求都必须包含用户名和使用基本 HTTP 身份验证标准的 FlightXML 密钥。
代码
var fxml_url = 'http://flightxml.flightaware.com/json/FlightXML2/';
var username = 'YOUR_USERNAME';
var apiKey = 'YOUR_APIKEY';
this.http.get(fxml_url + 'MetarEx', {
username: username,
password: apiKey,
query: {airport: 'KAUS', howMany: 1}
}).subscribe(data=>{
console.log(data)
})
但我得到的错误是
No overload matches this call.
The last overload gave the following error.
Argument of type '{ username: string; password: string; query: { airport: string; howMany: number; }; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.
Object literal may only specify known properties, and 'username' does not exist in type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.
有什么想法吗?
解决方案
来自文档:但是,对于某些库,可能需要在 base64 中手动编码“user:key”并将结果作为每个 HTTP 请求的一部分发送到“Authorization”标头中
var fxml_url = 'http://flightxml.flightaware.com/json/FlightXML2/';
var username = 'YOUR_USERNAME';
var apiKey = 'YOUR_APIKEY';
var auth = btoa(`${username}:${apiKey}`);
this.http.get(fxml_url + 'MetarEx', {
headers: { Authorization: auth },
params: new HttpParams({fromObject: { airport: 'KAUS', howMany: '1' }})
}).subscribe(data=>{
console.log(data)
});
推荐阅读
- awesome-wm - 您可以将客户端放入 awesome-wm 中的小部件层次结构吗?
- html - 循环遍历列表并根据 Angular 中的奇数或偶数插入 html 元素
- jquery - 一种排除特定数组的方法
- debugging - 调试器盒子里面有什么以及它是如何工作的
- ajax - Laravel - 获取复选框值并更新数据库
- php - 如何使用 php curl 进行双向/双向身份验证?
- java - 映射到 POJO 的 ObjectMapper 返回 Null
- r - 按功能汇总信息
- typescript - ts-node 和 ts-node-dev 不适用于 `cannot read property of undefined`,它可能不是循环导入
- sql - 基于相同的 Id 导出不同的名称