要求我試圖找回從Statbureau通脹數據API - https://www.statbureau.org/en/inflation-api採用了棱角分明2.我開始斷不僅是測試我的API,並確保它在控制檯輸出一個成功的消息。看來我使用的api是JSONP,因爲我使用的JSON地址是http://www.statbureau.org/calculate-inflation-price-jsonp?jsoncallback=?在網站上,他們提供了一個JS小提琴,它使用jquery對api進行了成功的調用。當我試圖在角2我收到以下錯誤相同的呼叫:HTTP GET在角2
TypeScript error: C:/Users/Rennie/projects/inflator/app/pages/home/home.ts(22,13): Error TS
2322: Type 'Observable<any>' is not assignable to type 'HomePage'.
Property 'jsonp' is missing in type 'Observable<any>'.
TypeScript error: C:/Users/Rennie/projects/inflator/app/pages/home/home.ts(22,13): Error TS
2409: Return type of constructor signature must be assignable to the instance type of the c
這裏是我的代碼
import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {Jsonp, URLSearchParams } from '@angular/http';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
@Component({
templateUrl: 'build/pages/home/home.html'
})
export class HomePage {
constructor(private jsonp: Jsonp) {
this.jsonp=jsonp;
let cpiUrl = "https://www.statbureau.org/calculate-inflation-price-jsonp?jsoncallback"
let params = new URLSearchParams();
params.set('country', 'united-states');
params.set('amount', '102');
params.set('start', '1968/1/1');
params.set('finish', '2016/1/1');
// TODO: Add error handling
return this.jsonp
.get(cpiUrl, { search: params }).map(function(response){ return response.json(); })
}
}
我的代碼從的jsfiddle電話不同,因爲我硬編碼api參數只是爲了快速看到結果。
我轉發了這個,因爲一個新的錯誤,看一看 - http://stackoverflow.com/questions/38401935/jsonp-request-error-angular-2 – RSB