gpt4 book ai didi

typescript - Angular 2 Http – 如何使用 finance_charts_json_callback() 回调从 API 获取 JSON 数据

转载 作者:太空狗 更新时间:2023-10-29 17:50:51 25 4
gpt4 key购买 nike

我正在尝试从此 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(App, [JSONP_PROVIDERS]) 引导应用程序并从 angular2/http 导入 Jsonp 服务模块。

关于typescript - Angular 2 Http – 如何使用 finance_charts_json_callback() 回调从 API 获取 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35233604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com