typescript Angular 2 Http – 如何使用 Finance_charts_json_callback() 回调从 API 获取 JSON 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35233604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular 2 Http – How to Get JSON Data from API with finance_charts_json_callback() callback
提问by Hunter
I'm trying to get json data from this api: http://chartapi.finance.yahoo.com/instrument/1.0/NFLX/chartdata;type=quote;range=1d/jsonAnd I don't know how to get into the returned finance_charts_json_callback().
我正在尝试从此 api 获取 json 数据:http://chartapi.finance.yahoo.com/instrument/1.0/NFLX/chartdata;type=quote;range=1d/json 而我不知道如何获取进入返回的finance_charts_json_callback()。
I'm using Angular 2's http.get():
我正在使用 Angular 2 的 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:
当它到达时=> res.json()
,它会抛出这个错误:
EXCEPTION: SyntaxError: Unexpected token i
例外:语法错误:意外的标记 i
回答by dfsq
You need to use JSONP in this case with callback name JSONP_CALLBACK:
在这种情况下,您需要使用回调名称为 JSONP_CALLBACK 的 JSONP:
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.
url
应该在哪里http://chartapi.finance.yahoo.com/instrument/1.0/NFLX/chartdata;type=quote;range=1d/json/?callback=JSONP_CALLBACK
,注意callback=JSONP_CALLBACK
部分。
And of course, remember to bootstrap the app with bootstrap(App, [JSONP_PROVIDERS])
and import Jsonp
service from angular2/http
module.
当然,请记住使用模块引导应用程序bootstrap(App, [JSONP_PROVIDERS])
并导入Jsonp
服务angular2/http
。