本文介绍了JSONP请求错误Angular 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a jsonp request in angular 2. I get a response when I click on the link of the error message, however I am unable to output this response to the browser, I get the error:

未捕获状态响应:200确认URL:

Uncaught Response with status: 200 Ok for URL: 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();
               })

    }


}


推荐答案

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

    }


}

这篇关于JSONP请求错误Angular 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-02 22:31