Angular 2 Http - How to get JSON data from an API using finance_charts_json_callback () callback

I am 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 file ().

I am using Angular 2 http.get ():

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

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

EXCEPTION: Syntax Error: Unexpected token i

+4
source share
1 answer

You need to use JSONP in this case with the callback name 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]) Jsonp angular2/http.

+5

Source: https://habr.com/ru/post/1627508/


All Articles