teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

追加

2018/07/04 08:53

投稿

sixth13
sixth13

スコア33

title CHANGED
File without changes
body CHANGED
@@ -9,17 +9,45 @@
9
9
  追記:
10
10
 
11
11
  aaa.comとbbb.comはドメイン・サーバーどちらも自分の管理下にある状態です。
12
+ 下記で200のコードは取得できますが無いページ(404 not found)はクロスオリジンエラーが出てしまいます。
12
13
 
14
+ bbb.comの.htaccessに下記を追加
15
+ ```.htaccess
16
+ header append Access-Control-Allow-Origin:*
17
+ ```
18
+
19
+ aaa.comの下記のjavaScriptを動かす
13
20
  ```javaScript
14
- // aaa.comの中のscript.js
21
+ var root_path = location.pathname;
22
+ var OTHER_SITE_DOMAIN = "https://bbb.com";
23
+ var otherSiteSameContentUrl = OTHER_SITE_DOMAIN + root_path;
24
+ console.log(otherSiteSameContentUrl);
15
- var xhr = new XMLHttpRequest();
25
+ var xhr = new XMLHttpRequest();
16
- xhr.onreadystatechange = function(){
17
- console.log(this.status);
26
+ console.log('UNSENT', xhr.status);
18
- }
19
- };
20
27
 
21
- xhr.open('GET', 'https://bbb.com/bbb.html', true);
28
+ xhr.open('GET', otherSiteSameContentUrl, true);
29
+ console.log('OPENED', xhr.status);
30
+
31
+ xhr.onprogress = function() {
32
+ console.log('LOADING', xhr.status);
33
+ };
34
+
35
+ xhr.onload = function() {
36
+ console.log('DONE', xhr.status);
37
+ $('body').append('DONE: ' + xhr.status);
38
+ };
39
+
22
- xhr.send();
40
+ xhr.send(null);
41
+
42
+ /**
43
+ * Outputs the following:
44
+ *
45
+ * UNSENT 0
46
+ * OPENED 0
47
+ * LOADING 200
48
+ * DONE 200
49
+ */
50
+
23
51
  ```
24
52
 
25
- 上記のthis.status]を取得いで
53
+ 無いページにアクセスした時にxhr.status]に404が出るようにするにはどのようにすればよろしいでしょうか

2

説明の追記

2018/07/04 08:53

投稿

sixth13
sixth13

スコア33

title CHANGED
File without changes
body CHANGED
@@ -8,4 +8,18 @@
8
8
 
9
9
  追記:
10
10
 
11
- aaa.comとbbb.comはドメイン・サーバーどちらも自分の管理下にある状態です。
11
+ aaa.comとbbb.comはドメイン・サーバーどちらも自分の管理下にある状態です。
12
+
13
+ ```javaScript
14
+ // aaa.comの中のscript.js
15
+ var xhr = new XMLHttpRequest();
16
+ xhr.onreadystatechange = function(){
17
+ console.log(this.status);
18
+ }
19
+ };
20
+
21
+ xhr.open('GET', 'https://bbb.com/bbb.html', true);
22
+ xhr.send();
23
+ ```
24
+
25
+ 上記の[this.status]を取得したいです。

1

説明の追記

2018/07/04 01:37

投稿

sixth13
sixth13

スコア33

title CHANGED
File without changes
body CHANGED
@@ -4,4 +4,8 @@
4
4
  レスポンスコードを受け取る事は可能でしょうか。
5
5
 
6
6
  また可能な場合どのように書けばよろしいでしょうか。
7
- ご教示いただければ幸いです。
7
+ ご教示いただければ幸いです。
8
+
9
+ 追記:
10
+
11
+ aaa.comとbbb.comはドメイン・サーバーどちらも自分の管理下にある状態です。