JSONP请求错误Angular 2

时间:2016-07-15 17:24:31

标签: javascript angular jsonp ionic2

我正在以角度2发出jsonp请求。当我点击错误消息的链接时我得到了响应,但是我无法将此响应输出到浏览器,我收到错误:

未审核状态响应状态:200确认网址为:https://www.statbureau.org/calculate-inflation-price-jsonp?jsoncallback&country=united-states&amount=102&start=1968%2F1%2F1&end=2016%2F1%2F1

import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {Jsonp, URLSearchParams } from '@angular/http';
import {JSONP_PROVIDERS} from '@angular/http';
@Component({
  templateUrl: 'build/pages/home/home.html',
  providers: [JSONP_PROVIDERS]
})
export class HomePage {

  value: any;
  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('end', '2016/1/1');
      params.set('format', 'true');
        // TODO: Add error handling
         this.jsonp
               .request(cpiUrl, { method: 'Get', search: params }).subscribe(res => { this.value = res.json();
               })

    }


}

1 个答案:

答案 0 :(得分:0)

import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {URLSearchParams } from '@angular/http';
import {Http, HTTP_PROVIDERS} from '@angular/http';
@Component({
  templateUrl: 'build/pages/home/home.html',
  providers: [HTTP_PROVIDERS]
})
export class HomePage {

  value: any;
  constructor(private http: Http) {
    this.http=http;
        let cpiUrl = "https://www.statbureau.org/calculate-inflation-price-json?"
         let params = new URLSearchParams();
      params.set('country', 'united-states');
      params.set('amount', '102');
        params.set('start', '1968/1/1');
        params.set('end', '2016/1/1');
      params.set('format', 'true');
        // TODO: Add error handling
         http.get(cpiUrl, {search: params }).subscribe(res => { this.value = res.text();
               })

    }


}