のための200 [OK]を私は角度2を使用してJSONP呼び出しをしようと、この記事で言及したすべてのものを踏襲しています:ここでHow to make a simple JSONP asynchronous request in Angular 2?角度2:例外を与えるJSONPリクエスト:ステータス応答:URL
は私service.tsです:
import { Injectable } from '@angular/core';
import { Http, Jsonp } from '@angular/http'
import 'rxjs';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class DataLoadService {
private quoteApiUrl: string;
constructor(private http: Http, private jsonp: Jsonp) {
this.quoteApiUrl = `http://api.forismatic.com/api/1.0/?method=getQuote&format=jsonp&jsonp=parseQuote&lang=en&callback=JSONP_CALLBACK`;
}
getQuotes(): Observable<any>{
return this.jsonp.get(this.quoteApiUrl)
.map(function(res){
return res.json() || {};
})
.catch(function(error: any){
return Observable.throw(error);
});
}
}
そして、これは私が私のhome.tsから私のgetQuotes()メソッドを呼び出しています方法です:
this.dataLoadService.getQuotes()
.subscribe(data => {
console.log(data);
this.quotes.push(data);
});
これはJSONPコールで、APIの最後に&callback=JSONP_CALLBACK
を追加しました。
parseQuote({"quoteText":"A failure is a man who has blundered but is not capable of cashing in on the experience. ", "quoteAuthor":"Elbert Hubbard", "senderName":"", "senderLink":"", "quoteLink":"http://forismatic.com/en/d74d7833cb/"})
しかし、私はまた、取得するには、次のエラー:
- キャッチされないにReferenceErrorサービスが呼び出されると
、私はChromeのデベロッパーツール>ネットワーク>応答]タブで有効なJSONレスポンスを得ますか:parseQuoteが定義されていません。
- 例外:ステータス付き応答:URL: http://api.forismatic.com/api/1.0/?method=getQuote&format=jsonp&jsonp=parseQuote&lang=en&parseQuote=ng_jsonp.__req0.finished
ご連絡ください。私は、角度v2.2.1を使用しています。
ありがとうございます。あなたの提案はうまくいった。コールバック関数名がコールバックパラメータではなく、jsonpパラメータに来る必要があることをどうお知りになりましたか。あなたにこれを伝えるエラーがありますか?これを共有することができれば、将来同様のシナリオで役立ちます。 –