简体   繁体   English

Http在Angular 2中获取请求

[英]Http get request in Angular 2

I am trying to retrieve data from the Statbureau inflation api- https://www.statbureau.org/en/inflation-api using Angular 2. I am starting off just testing out my api and making sure it outputs a success message in the console. 我正在尝试使用Angular 2从Statbureau通胀api- https://www.statbureau.org/en/inflation-api检索数据。我开始只是测试我的api,并确保它在api中输出成功消息。安慰。 It appears that the api I'm using is JSONP, because of the JSON address that I'm using http://www.statbureau.org/calculate-inflation-price-jsonp?jsoncallback= ? 看来我正在使用的api是JSONP,因为我正在使用http://www.statbureau.org/calculate-inflation-price-jsonp?jsoncallback=吗? On the website they provide a JS fiddle that makes a succesful call to the api using jquery. 在网站上,他们提供了一个JS小提琴,该小提琴使用jquery成功调用了api。 When I try to make the same call in Angular 2 I get the following errors: 当我尝试在Angular 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

Here is my code 这是我的代码

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(); })

    }


}

My code differs from the call in the jsfiddle because I hard coded the api parameters just to quickly see a result. 我的代码与jsfiddle中的调用不同,因为我对api参数进行了硬编码,只是为了快速查看结果。

1) A constructor typically (always?) does not have a return statement. 1)构造函数通常(总是?)没有return语句。 Removing the return statment should handle type 'Observable<any>' is not assignable to type 'HomePage'. 删除退货声明应将type 'Observable<any>' is not assignable to type 'HomePage'.

2) There does not seem to be a method get on the Jsonp class in Angular 2 check the docs . 2)似乎没有成为一个方法getJsonpAngular 2检查文档

3) An observable sequence/pipeline will not be executed until subscription (they are lazy), so even if that code did compile the request would not be executed. 3)直到订阅(它们是惰性的),才会执行可观察的序列/管道,因此即使该代码已编译,也不会执行该请求。

4) I think you should take a look at this similiar question. 4)我想你应该看看这个类似的问题。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM