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

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

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

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

当它到达 => res.json() 时,它抛出这个错误:

EXCEPTION: SyntaxError: Unexpected token i

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

的 JSONP
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 部分。

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