2016-10-05 11 views
0

私はJSONP get呼び出しを実行するための適切なメソッドが何であるか知っています。この例では、私のJSONPのURLはmyRunの下に表示されています(see in this link)。Angular 2 Http with jsonp

import { Injectable } from '@angular/core'; 
import { Http, Response } from '@angular/http'; 
import { Observable } from 'rxjs/Rx'; 

@Injectable() 
export class AppService { 
constructor(private http: Http){} 
    fetchData(){ 
    return this.http.get('http://dev.markitondemand.com/MODApis/Api/v2/Quote/jsonp?symbol=AAPL&callback=myRun').map(
     (res) => res.json() 
     ).subscribe(
     (data) => console.log(data) 
    ); 
    } 
} 

私はexamples that useJSONP_PROVIDERSを見てきましたが、私は、彼らは今では廃止されて信じています。したがって、私はJSONPがリクエストを取得するための最新の方法が何であるか疑問に思っています。

+1

この投稿をチェックするhttp://stackoverflow.com/questions/36289495/how-to-make-a-simple-jsonp-asynchronous-request-in-angular-2 –

答えて

1

リンク先の例と同じですが、しかし、JSONP_PROVIDERSを使用する代わりにHttpModuleのようにJsonpModuleをインポートします。次に、your linked exampleのようにJsonpを注入してください。その使用法は変更されていません。

export class AppComponent { 
    constructor(jsonp:Jsonp) { 
    var url = 'https://accounts.google.com/logout&c=JSONP_CALLBACK'; 
    jsonp.request(url, { method: 'Get' }) 
    .subscribe((res) => { 
     (...) 
    }); 
    } 
} 

前述のクラスのすべてのも@angular/httpからインポートすることができます。