私は、MDNからCORSポリシーを読んだ後で、さらにhttps://www.html5rocks.com/en/tutorials/cors/の以下のコードを使用して、できることをすべて試しました。私はちょうどwikiページ(与えられた)を取りたいと思った。それはonerrorメソッドを実行することを示すエラーメッセージを吐き出します。コンソールで、私は を印刷します。 "クロスオリジン要求がブロックされました:同じオリジンポリシーは、リモートリソースの読み取りをhttps://en.wikipedia.org/wiki/Main_Pageで許可しません(理由:CORSヘッダー 'Access-Control-Allow-Origin'がありません)。 。CORSポリシーを取得する
// 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('(.*)?')[1];
}
// Make the actual CORS request.
function makeCorsRequest() {
// This is a sample server that supports CORS.
// var url = 'http://html5rocks-cors.s3-website-us-east-1.amazonaws.com/index.html';
var url = 'https://en.wikipedia.org/wiki/Main_Page';
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();
}
上記のコードを使ってみたときに実際に何が起こったのかを実際に言った方がずっと助かります。すべての詳細を含めることで、質問が簡単に答えることができます。 –
投票を停止させた問題は何ですか? – user618677
あなたは[この時点で]読んでいるチュートリアルを放棄したようです(https://www.html5rocks.com/ja/tutorials/cors/#toc-adding-cors-support-to-the-server )。あなたは読書を止めてはいけません。 – Quentin