我正试图从这个API获取json数据:
http://chartapi.finance.yahoo.com/instrument/1.0/NFLX/chartdata;type=quote;range=1d/json
而且我不知道如何进入返回的finance_charts_json_callback().
而且我不知道如何进入返回的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并使用回调名称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部分.
当然,记得使用bootstrap(App,[JSONP_PROVIDERS])引导应用程序并从angular2 / http模块导入Jsonp服务.