2016-08-09 9 views
1

私はthisを見ましたが、いくつか質問があります。Angular2:要求されたリソースに 'Access-Control-Allow-Origin'ヘッダーがありません。 Origin 'null'はアクセスが許可されていません

は、私がこのURIからの応答を取得したい:

http://www.instagram.com/justinbieber/media/ 

をし、私はそれのために以下のコードを実装している:

import {Http,Headers} from '@angular/http'; 
import {Observable} from 'rxjs/Observable'; 
import 'rxjs/add/operator/map'; 
import {Component,Injectable} from '@angular/core'; 
import {Gallery} from './gallery';  

@Injectable() 
export class InstagramService{ 

    constructor(private _http:Http){    

    } 

    getGallery(username: string) : Observable<Gallery> { 

     return this._http.get("http://www.instagram.com/justinbieber/media/").map(res => res.json());                    
    } 
} 

Unfortunatllyを、私はこのサービスを呼び出すときに、それがで文句:

XMLHttpRequest cannot load https://www.instagram.com/justinbieber/media/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 

私にはいくつか質問があります:

1私のブラウザやpostmanの応答を得ることはできますが、私のコードではできません。この問題を引き起こすのは何ですか?

2 - コードに問題がある場合は、ご提案ください。

ヒント:このURIはjsonp.getもサポートしていません。

答えて

1

代わりにJSONPをサポートするInstagram APIを使用する必要があります。ここではサンプルです:

constructor(private jsonp:Jsonp) { 
    let url = 'https://api.instagram.com/v1/tags/ootd/media/recent?client_id=someidnumbersxxxxxxxxxxxxxx&callback=JSONP_CALLBACK'; 
    jsonp.request(url, { method: 'Get' }) 
     .subscribe((res) => { 
     (...) 
     }); 
} 

は詳細についてはこれらの質問を参照してください。

+1

問題は、それはあなたのAngular2アプリケーションでの問題ではないということです。 Postmanでは、クロムプラグインなので、物事は少し違うので、HTTP呼び出しに関する制限は同じではありません。受け取ったエラーを修正するには、サーバー側で何かを実行しなければならないので、Instagramまでです。 JSONPをサポートするのと同じことです。だから私はJSONPをサポートするInstagram APIを扱っています。あなたの質問に答えてほしいと思う;-) –

+1

おそらくこの記事はあなたに興味があるかもしれません:http://restlet.com/blog/2015/12/15/understanding-and-using-cors/。 –

+0

こちらの記事もご覧ください:http://restlet.com/blog/2016/09/27/how-to-fix-cors-problems/ –

関連する問題