LoginSignup
2
3

More than 5 years have passed since last update.

XMLHttpRequestを使ったときのエラー

Last updated at Posted at 2018-11-01

javascriptで現在のURLをもとにタイトルを取得しようとしたらエラーが出た。
その時のソースコードがこちら





var checkurl = location.href;
<!--URLからタイトルを取ってくるためのとこ-->
function getTitle(url,callback)
{
    var tit=new XMLHttpRequest()
    tit.onload = function()
    {
        var title=this.responseXML.title||"notitle"
        callback(url,title)
    }
    tit.open("GET", url, true);
    tit.responseType="document";
    tit.send();
}
<!--コンソールログにタイトル名表示するための場所-->
getTitle(checkurl,consoleTitle)
function consoleTitle(url,title)
{
console.log(title);
};

これをhtmlファイルのscriptタグの中に記入しgooglechromeで開くとコンソールログのほうでエラーが出ていた。
内容はセキュリティエラーのようだったのでコマンドプロンプトから以下を実行した。
cd C:\Program Files (x86)\Google\Chrome\Application
chrome.exe -allow-file-access-from-files
これで(多分)解決した。
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
ps.実際のエラーコードを載せておきます

横書きver



pagelog.html:27 Access to XMLHttpRequest at 'file:///F:/%E5%8D%92%E6%96%B0%E3%83%91%E3%83%BC%E3%83%84/pagelog.html' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

改行ver



pagelog.html:27 Access to XMLHttpRequest at 'file:///F:/%E5%8D%92%E6%96%B0%E3%83%91%E3%83%BC%E3%83%84/pagelog.html' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3