aaa.comとbbb.comがあったとして
aaa.comのjavaScriptに何らかのコードを書いてでbbb.comにアクセスして
bbb.comの中の任意のページ(html)が無い時に404等の
レスポンスコードを受け取る事は可能でしょうか。
また可能な場合どのように書けばよろしいでしょうか。
ご教示いただければ幸いです。
追記:
aaa.comとbbb.comはドメイン・サーバーどちらも自分の管理下にある状態です。
下記で200のコードは取得できますが無いページ(404 not found)はクロスオリジンエラーが出てしまいます。
bbb.comの.htaccessに下記を追加
header append Access-Control-Allow-Origin:*
aaa.comの下記のjavaScriptを動かす
javaScript
1var root_path = location.pathname; 2var OTHER_SITE_DOMAIN = "https://bbb.com"; 3var otherSiteSameContentUrl = OTHER_SITE_DOMAIN + root_path; 4console.log(otherSiteSameContentUrl); 5 var xhr = new XMLHttpRequest(); 6 console.log('UNSENT', xhr.status); 7 8 xhr.open('GET', otherSiteSameContentUrl, true); 9 console.log('OPENED', xhr.status); 10 11 xhr.onprogress = function() { 12 console.log('LOADING', xhr.status); 13 }; 14 15 xhr.onload = function() { 16 console.log('DONE', xhr.status); 17 $('body').append('DONE: ' + xhr.status); 18 }; 19 20 xhr.send(null); 21 22 /** 23 * Outputs the following: 24 * 25 * UNSENT 0 26 * OPENED 0 27 * LOADING 200 28 * DONE 200 29 */ 30
無いページにアクセスした時に[xhr.status]に404が出るようにするにはどのようにすればよろしいでしょうか。
回答3件
あなたの回答
tips
プレビュー