4

I'm 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().

I'm using Angular 2's 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:

EXCEPTION: SyntaxError: Unexpected token i

2

1 Answer 1

6

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

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.

And of course, remember to bootstrap the app with bootstrap(App, [JSONP_PROVIDERS]) and import Jsonp service from angular2/http module.

Sign up to request clarification or add additional context in comments.

4 Comments

is not JSONP_CALLBACK in the url he's trying to get is finance_charts_json_callback
may i know what is JSONP i heared about JSON, JSONP term is new for me !
@Langley No, finance_charts_json_callback is a default wrapper callback used by API if you don't specify real one. For Angular machinery it is required response to be wrapped into JSONP_CALLBACK.
@PardeepJain Take a look at this thread, nice explanations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.