2016-06-22 4 views
-2

JavaScriptでHTTPS URLからlocalhostへのデータ取得にCORSを使用するにはどうすればよいですか? URLはJSONオブジェクトとして処理する必要のあるService-Nowインシデントリストです。JavascriptでCORSを使用するには?

答えて

0

CORSは、新しいブラウザがサポートするサーバー側の機能です。

クロスドメインサービスからリソースを要求すると、ブラウザはサーバへのプリフライトリクエストを実行し、アクセス情報を要求します。

サーバーが別のドメインのリソースにアクセスできるようにすると、HTTPヘッダーがいくつか返され、ブラウザーはそれらのヘッダーをアクションによって決めることになります。

続きを読むhere。ここで

0

は、あなたがそれを行う方法では、提供サーバ側の設定が行われます。 'URL' をロードすることはできませんhttp://www.html5rocks.com/en/tutorials/cors/

+0

のXMLHttpRequest:

// Create the XHR object. function createCORSRequest(method, url) { var xhr = new XMLHttpRequest(); if ("withCredentials" in xhr) { // XHR for Chrome/Firefox/Opera/Safari. xhr.open(method, url, true); } else if (typeof XDomainRequest != "undefined") { // XDomainRequest for IE. xhr = new XDomainRequest(); xhr.open(method, url); } else { // CORS not supported. xhr = null; } return xhr; } // Helper method to parse the title tag from the response. function getTitle(text) { return text.match('<title>(.*)?</title>')[1]; } // Make the actual CORS request. function makeCorsRequest() { // All HTML5 Rocks properties support CORS. var url = 'http://updates.html5rocks.com'; var xhr = createCORSRequest('GET', url); if (!xhr) { alert('CORS not supported'); return; } // Response handlers. xhr.onload = function() { var text = xhr.responseText; var title = getTitle(text); alert('Response from CORS request to ' + url + ': ' + title); }; xhr.onerror = function() { alert('Woops, there was an error making the request.'); }; xhr.send(); } 

これはからです。要求されたリソースに「Access-Control-Allow-Origin」ヘッダーが存在しません。したがって、 'http:// localhost:xyz'はアクセスできません。応答にHTTPステータスコード401がありました。 これは上記のコードを実行している間に発生しているエラーです。 –

関連する問題