首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Http请求在角2中

Http请求在角2中
EN

Stack Overflow用户
提问于 2016-07-14 18:09:46
回答 1查看 1.2K关注 0票数 0

我正在尝试使用角2从Statbureau通胀api- https://www.statbureau.org/en/inflation-api中检索数据。我刚开始测试我的api并确保它在控制台中输出一个成功的消息。看来,我使用的api是JSONP,因为我在网站上使用http://www.statbureau.org/calculate-inflation-price-jsonp?jsoncallback=的JSON地址,他们提供了一个JS小提琴,使用jquery对api进行了成功的调用。当我试图在角2中进行同样的调用时,我会得到以下错误:

代码语言:javascript
复制
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

这里是我的代码

代码语言:javascript
复制
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参数只是为了快速看到结果。

EN

回答 1

Stack Overflow用户

发布于 2016-07-15 06:07:29

1)构造函数通常(总是?)没有返回语句。移除返回状态应该处理type 'Observable<any>' is not assignable to type 'HomePage'.

2)在getJsonp类上似乎没有检查文档的方法。

3)在订阅之前不会执行可观察的序列/管道(它们很懒惰),因此即使代码编译了请求,也不会执行请求。

4)我认为你应该看看相似的问题。

祝好运。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38381296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档