Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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

share|improve this question
    
if you log out the res object without calling the .json method on it what is returned? – inoabrian Feb 5 at 22:12
    
the response you are getting is not json, is something called jsonp, try to use use this instead: angular.io/docs/js/latest/api/http/Jsonp-class.html, an example here: stackoverflow.com/questions/33675842/… – Langley Feb 5 at 22:55
up vote 0 down vote accepted

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.

share|improve this answer
    
is not JSONP_CALLBACK in the url he's trying to get is finance_charts_json_callback – Langley Feb 5 at 23:23
    
may i know what is JSONP i heared about JSON, JSONP term is new for me ! – Pardeep Jain Feb 6 at 5:21
    
@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. – dfsq Feb 6 at 8:26
    
@PardeepJain Take a look at this thread, nice explanations. – dfsq Feb 6 at 8:27
    
@dfsq got it, thanks ! – Langley Feb 8 at 19:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.