2017-01-25 6 views
1

私はbit.ly APIを動作させようとしていますが、CORSの問題で立ち往生しています。私ができるすべての情報源をチェックしたが、問題は残っている。Angular2 bit.ly 'Access-Control-Allow-Origin'ヘッダーの問題

Angular2コード:クロームネットワークから

getBitLyUrl (fullUrl : string): Observable<string> { 


    let headers = new Headers({'Access-Control-Allow-Origin' : '*', 'Accept' : 'application/json' }); 
    let options = new RequestOptions({ headers: headers }); 


    let res = this._http.get('https://api-ssl.bitly.com/v3/shorten?access_token=a4d0c60f2cfdba6941b9012c76d95a06f8b3765e&longUrl=' 
        + fullUrl + '&format=json', options) 
     .map((response: Response) => response.json()); 

    return res; 
} 

ヘッダ:

General: 
Request URL:https://api-ssl.bitly.com/v3/shorten?  access_token=a4d0c60f2cfdba6941b9012c76d95a06f8b3765e&longUrl=http://www.cyberma .net&format=json 
Request Method:OPTIONS 
Status Code:200 OK 
Remote Address:67.199.248.20:443 


Response: 
Allow:GET, POST, OPTIONS 
Connection:keep-alive 
Content-Length:0 
Content-Type:text/plain; charset=utf-8 
Date:Wed, 25 Jan 2017 10:22:31 GMT 
Server:nginx 

Request: 
Accept:*/* 
Accept-Encoding:gzip, deflate, sdch, br 
Accept-Language:en,cs;q=0.8,sk;q=0.6,de;q=0.4 
Access-Control-Request-Headers:access-control-allow-origin, x-xsrf-token 
Access-Control-Request-Method:GET 
Cache-Control:max-age=0 
Connection:keep-alive 
Host:api-ssl.bitly.com 
Origin:http://localhost:3000 
Referer:http://localhost:3000/ 
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36 

私はこのエラーを取得する:

XMLHttpRequest cannot load https://api-ssl.bitly.com/v3/shorten?access_token=a4d0c60f2cfdba6941b9012c76d95a06f8b3765e&longUrl=http://www.cyberma.net&format=json . Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:3000 ' is therefore not allowed access.

すべてのヘルプは非常にいただければ幸いです!

答えて

0

let headers = new Headers({'Access-Control-Allow-Origin' : '*', 'Accept' : 'application/json' });。これは応答ヘッダーです。 CORSは、バックエンドサーバーを安全に保護するために設定されているため、これらのヘッダーは応答としてバックエンドサーバーによって設定する必要があります。サーバーを制御できない場合、ドメインを承認しない限り、クロスオリジンからアクセスするオプションはありません。

OPTIONSリクエストは角度ではなくブラウザによって設定されます。詳しくは、hereをご覧ください。

The Cross-Origin Resource Sharing standard works by adding new HTTP headers that allow servers to describe the set of origins that are permitted to read that information using a web browser. Additionally, for HTTP request methods that can cause side-effects on user data (in particular; for HTTP methods other than GET, or for POST usage with certain MIME types). The specification mandates that browsers "preflight" the request, soliciting supported methods from the server with an HTTP OPTIONS request header, and then, upon "approval" from the server, sending the actual request with the actual HTTP request method. Servers can also notify clients whether "credentials" (including Cookies and HTTP Authentication data) should be sent with requests.

+0

get関数からヘッダとオプションの引数を削除します。 https://dev.bitly.com/cors.html 例と私の違いは、角度からの要求がOptionで始まることです。彼らの例がGETを送信している間。 –

+0

私は自分の答えを更新しました。 – raj

+1

'let headers = new Headers({'アクセス制御許可元 ':' * '、' Accept ':'アプリケーション/ json '}); '行を削除してリクエストが機能します。ここでの答えが指摘しているように、 'Access-Control-Allow-Origin'はリクエストヘッダーではなく、* response *ヘッダーです。それでは、依頼でそれを送信しないでください。これをリクエストに含めると、実際には[GETの代わりにあなたのブラウザにOPTIONSプリフライトリクエストを送信させる](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests)があります。そして 'Accept:application/json'を送っても問題ありませんが、リクエストはそれもなくうまく動作します。 – sideshowbarker

0

ちょうど私は、プレーンJSを使用して、その例をしようとすると、それがうまく機能しない、ラジ