Angular 2 Http –如何使用finance_charts_json_callback()回调从API获取JSON数据 [英] Angular 2 Http – How to Get JSON Data from API with finance_charts_json_callback() callback

查看:82
本文介绍了Angular 2 Http –如何使用finance_charts_json_callback()回调从API获取JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从此api获取json数据: http://chartapi.finance.yahoo.com/instrument/1.0/NFLX/chartdata;type=quote;range=1d/json 而且我不知道如何进入返回的Finance_charts_json_callback().

I'm trying to get json data from this api: http://chartapi.finance.yahoo.com/instrument/1.0/NFLX/chartdata;type=quote;range=1d/json And I don't know how to get into the returned finance_charts_json_callback().

我正在使用Angular 2的http.get():

I'm using Angular 2's http.get():

loadData() {
  return this.http
     .get(this.url)
     .map((res) => res.json())
     .subscribe((data) => console.log(data));
}

到达=> res.json()时,抛出此错误:

例外:SyntaxError:意外的令牌i

EXCEPTION: SyntaxError: Unexpected token i

推荐答案

在这种情况下,您需要使用具有回调名称JSONP_CALLBACK的JSONP:

You need to use JSONP in this case with callback name JSONP_CALLBACK:

loadData() {
    this.jsonp.get(this.url)
        .map(res => res.json())
        .subscribe(data => console.log(data));
}

url应该为http://chartapi.finance.yahoo.com/instrument/1.0/NFLX/chartdata;type=quote;range=1d/json/?callback=JSONP_CALLBACK的地方,请注意callback=JSONP_CALLBACK部分.

Where url should be http://chartapi.finance.yahoo.com/instrument/1.0/NFLX/chartdata;type=quote;range=1d/json/?callback=JSONP_CALLBACK, note callback=JSONP_CALLBACK part.

当然,请记住使用bootstrap(App, [JSONP_PROVIDERS])引导应用程序,并从angular2/http模块导入Jsonp服务.

And of course, remember to bootstrap the app with bootstrap(App, [JSONP_PROVIDERS]) and import Jsonp service from angular2/http module.

这篇关于Angular 2 Http –如何使用finance_charts_json_callback()回调从API获取JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆