Hunter Hunter - 1 year ago 159
TypeScript Question

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

I'm trying to get json data from this api:;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
.map((res) => res.json())
.subscribe((data) => console.log(data));

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

EXCEPTION: SyntaxError: Unexpected token i

Answer Source

You need to use JSONP in this case with callback name JSONP_CALLBACK:

loadData() {
        .map(res => res.json())
        .subscribe(data => console.log(data));

Where url should be;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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download