Angular 2 Http – How to Get JSON Data from API with finance_charts_json_callback() callback

断了今生、忘了曾经 提交于 2019-12-01 22:04:58

问题


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().

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

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

When it gets to => res.json(), it throws this error:

EXCEPTION: SyntaxError: Unexpected token i


回答1:


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

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.

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



来源:https://stackoverflow.com/questions/35233604/angular-2-http-how-to-get-json-data-from-api-with-finance-charts-json-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!