Calling the Twitch API with Angular 2 -
i’m trying first http call 3rd party api in angular 2 (2.1.0) isn't following tutorial, , i'm pretty stuck.
i’m trying call twitch api freecodecamp challenge (which pretty painful in itself). i’ve taken api call working example written in jquery, know i’m trying call right place, can’t seem data back.
my service has following:
apibaseurl: string = 'https://wind-bow.hyperdev.space/twitch-api/streams/'; user: string = 'esl_sc2'; query: string = '?callback=?'; constructor(public jsonp: jsonp) { } refresh(): observable<string> { const apicall: string = this.apibaseurl + this.user + this.query; return this.jsonp.get(apicall) .map((res: response) => res.json()) .catch((err: any) => observable.throw(err.json().err)); } and i’m subscribed in component this:
ngoninit() { this.twitchservice.refresh().subscribe( val => this.val = val, error => this.errormessage = <any>error); } but in console, have error
uncaught syntaxerror: unexpected token ===
when click on error in sources tab of dev tools:
/**/ typeof === 'function' && ({"stream":{"_id":23623614656,"game":"starcraft ii","viewers":283,... (etc.) so looks i’m getting data api, cannot work out why it’s not coming component properly. if can shed light on this, appreciate it.
there's 1 thing need work. define jsonp_callback on querystring callback parameter:
query: string = '?callback=jsonp_callback'; you extremely close! found out here - how make simple jsonp asynchronous request in angular 2?
and plunker demonstrating change , result: http://plnkr.co/edit/qgbecxycfersx2btujq6?p=preview
Comments
Post a Comment