質問編集履歴

4

誤った表記

2016/03/04 05:24

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -236,10 +236,6 @@
236
236
 
237
237
 
238
238
 
239
- <?php
240
-
241
-
242
-
243
239
  header("Content-type: application/json; charset=utf-8");
244
240
 
245
241
  if(isset($_POST['request'])){

3

errorThrown:SyntaxError:Unexpected token O

2016/03/04 05:24

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -6,288 +6,276 @@
6
6
 
7
7
  よろしくおねがいします。
8
8
 
9
- サーバーはローカル開発環境でXAMPPを使っています。
10
-
11
9
  当方、PHP、Ajaxをほとんど触ったことがないためわかりやすい解説をお願いします。
12
10
 
13
11
 
14
12
 
15
13
  ###発生している問題・エラーメッセージ
16
14
 
17
- XMLHttpRequest.status:404
18
-
19
-
20
-
21
- XMLHttpRequest.responseText:<html>
22
-
23
- <head><tittle>404 Not Found</title></head>
24
-
25
- <body bgcolor="white">
26
-
27
- <center><h1>404 Not Found</h1></center>
28
-
29
- <hr><center>nginx</center>
15
+ XMLHttpRequest.status:200
16
+
17
+ XMLHttpRequest.responseText:OK
18
+
19
+ textStatus:textStatus:parsererror → phpの戻り値がjson形式になっていなかったことが原因
20
+
21
+ errorThrown:SyntaxError:Unexpected token O
22
+
23
+
24
+
25
+ errorThrown:OK
26
+
27
+ ###ソースコード
28
+
29
+ ```HTML
30
+
31
+ <!DOCTYPE HTML>
32
+
33
+ <html ng-csp>
34
+
35
+ <head>
36
+
37
+ <meta charset="utf-8">
38
+
39
+ <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
40
+
41
+ <meta http-equiv="Content-Security-Policy" content="default-src * data:; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'">
42
+
43
+ <script src="components/loader.js"></script>
44
+
45
+ <script src="js/winstore-jscompat.js"></script>
46
+
47
+ <link rel="stylesshet" href="components/monaca-onsenui/js/angular/angular-csp.css">
48
+
49
+ <link rel="stylesheet" href="components/loader.css">
50
+
51
+ <link rel="stylesheet" href="css/style.css">
52
+
53
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min/js"></script>
54
+
55
+ <meta http-equiv="Content-Security-Policy" content="default-src *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'">
56
+
57
+ <script>
58
+
59
+
60
+
61
+ ons.bootstrap();
62
+
63
+
64
+
65
+ ons.ready(function getTxt(event){
66
+
67
+
68
+
69
+ $.ajax("load.txt", {
70
+
71
+ async: true,
72
+
73
+ cache:false,
74
+
75
+ success: function(data, status, jqXHR){
76
+
77
+
78
+
79
+ var data_array = data.split(/\r\n|\r|\n/);
80
+
81
+ var len = data_array.length;
82
+
83
+
84
+
85
+ $("output:first").text(data);
86
+
87
+
88
+
89
+ $('#hoge').empty();
90
+
91
+
92
+
93
+ for(var i=0; i<len; i++){
94
+
95
+ $('<label class="checkbox checkbox--list-item" ><input type="checkbox" name="checkbox"value="' + data_array[i] + '"/><div class="checkbox__checkmark checkbox--list-item__checkmark"></div>' + data_array[i] + '</label>').appendTo('#hoge');
96
+
97
+ }
98
+
99
+
100
+
101
+ }
102
+
103
+ });
104
+
105
+ });
106
+
107
+
108
+
109
+ function chkvalue(){
110
+
111
+
112
+
113
+ //チェックボックスのデータ取得
114
+
115
+
116
+
117
+ var chkVal = [];
118
+
119
+ $('[name="checkbox"]:checked').each(function(){
120
+
121
+ chkVal.push($(this).val());
122
+
123
+ });
124
+
125
+
126
+
127
+ var data = {'recuest':JSON.stringify(chkVal)};
128
+
129
+
130
+
131
+ $.ajax({
132
+
133
+ type:"post",
134
+
135
+ url:"/php/sample/send.php",
136
+
137
+ data:data,
138
+
139
+ crossDomain: true,
140
+
141
+ dataType:"json",
142
+
143
+ scriptCharset: "utf-8",
144
+
145
+ success:function(data){
146
+
147
+ alert(data.text)
148
+
149
+ },
150
+
151
+ error: function(XMLHttpRequest, textStatus, errorThrown){
152
+
153
+ alert("XMLHttpRequest.status:"+XMLHttpRequest.status);
154
+
155
+ alert("XMLHttpRequest.responseText:"+XMLHttpRequest.responseText);
156
+
157
+ alert("textStatus:"+textStatus);
158
+
159
+ alert("errorThrown:"+errorThrown);
160
+
161
+ }
162
+
163
+ });
164
+
165
+ }
166
+
167
+
168
+
169
+ </script>
170
+
171
+ </head>
172
+
173
+ <body>
174
+
175
+
176
+
177
+ <div style="text-align: center" class="check">
178
+
179
+
180
+
181
+ <!-- 取得したテキストの表示 --><br />
182
+
183
+ <output></output><br /><br />
184
+
185
+
186
+
187
+ <!-- チェックボックスの表示 -->
188
+
189
+ <ons-list>
190
+
191
+
192
+
193
+ <ons-list-header>CheckBox</ons-list-header>
194
+
195
+
196
+
197
+ <ons-list-item modifier="tappable">
198
+
199
+ <li id="hoge"></li>
200
+
201
+ </ons-list-item>
202
+
203
+
204
+
205
+ </ons-list>
206
+
207
+
208
+
209
+ <!-- ボタン表示 -->
210
+
211
+ <ons-button onclick="chkvalue()">Click to Check</ons-button>
212
+
213
+ </div>
214
+
215
+
216
+
217
+ </ons-page>
218
+
219
+ </ons-navigator>
220
+
221
+ </div>
222
+
223
+ </ons-sliding-menu>
224
+
225
+
30
226
 
31
227
  </body>
32
228
 
33
229
  </html>
34
230
 
35
- <!--a padding to disable MSIE and Chrome friendly error page -->
36
-
37
- <!--a padding to disable MSIE and Chrome friendly error page -->
38
-
39
- <!--a padding to disable MSIE and Chrome friendly error page -->
40
-
41
- <!--a padding to disable MSIE and Chrome friendly error page -->
42
-
43
- <!--a padding to disable MSIE and Chrome friendly error page -->
44
-
45
- <!--a padding to disable MSIE and Chrome friendly error page -->
46
-
47
-
48
-
49
- textStatus:error
50
-
51
-
52
-
53
- errorThrown:OK
54
-
55
- ###ソースコード
56
-
57
- ```HTML
58
-
59
- <!DOCTYPE HTML>
60
-
61
- <html ng-csp>
62
-
63
- <head>
64
-
65
- <meta charset="utf-8">
66
-
67
- <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
68
-
69
- <meta http-equiv="Content-Security-Policy" content="default-src * data:; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'">
70
-
71
- <script src="components/loader.js"></script>
72
-
73
- <script src="js/winstore-jscompat.js"></script>
74
-
75
- <link rel="stylesshet" href="components/monaca-onsenui/js/angular/angular-csp.css">
76
-
77
- <link rel="stylesheet" href="components/loader.css">
78
-
79
- <link rel="stylesheet" href="css/style.css">
80
-
81
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min/js"></script>
82
-
83
- <meta http-equiv="Content-Security-Policy" content="default-src *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'">
84
-
85
- <script>
86
-
87
-
88
-
89
- ons.bootstrap();
90
-
91
-
92
-
93
- ons.ready(function getTxt(event){
94
-
95
-
96
-
97
- $.ajax("load.txt", {
98
-
99
- async: true,
100
-
101
- cache:false,
102
-
103
- success: function(data, status, jqXHR){
104
-
105
-
106
-
107
- var data_array = data.split(/\r\n|\r|\n/);
108
-
109
- var len = data_array.length;
110
-
111
-
112
-
113
- $("output:first").text(data);
114
-
115
-
116
-
117
- $('#hoge').empty();
118
-
119
-
120
-
121
- for(var i=0; i<len; i++){
122
-
123
- $('<label class="checkbox checkbox--list-item" ><input type="checkbox" name="checkbox"value="' + data_array[i] + '"/><div class="checkbox__checkmark checkbox--list-item__checkmark"></div>' + data_array[i] + '</label>').appendTo('#hoge');
124
-
125
- }
126
-
127
-
128
-
129
- }
130
-
131
- });
132
-
133
- });
134
-
135
-
136
-
137
- function chkvalue(){
138
-
139
-
140
-
141
- //チェックボックスのデータ取得
142
-
143
-
144
-
145
- var chkVal = [];
146
-
147
- $('[name="checkbox"]:checked').each(function(){
148
-
149
- chkVal.push($(this).val());
150
-
151
- });
152
-
153
-
154
-
155
- var data = chkVal[0];
156
-
157
-
158
-
159
- $.ajax({
160
-
161
- type:"post",
162
-
163
- url:"/php/sample/send.php",
164
-
165
- data:JSON.stringify(data),
166
-
167
- crossDomain: true,
168
-
169
- dataType:"json",
170
-
171
- scriptCharset: "utf-8",
172
-
173
- success:function(data){
174
-
175
- alert(data.text)
176
-
177
- },
178
-
179
- error: function(XMLHttpRequest, textStatus, errorThrown){
180
-
181
- alert("XMLHttpRequest.status:"+XMLHttpRequest.status);
182
-
183
- alert("XMLHttpRequest.responseText:"+XMLHttpRequest.responseText);
184
-
185
- alert("textStatus:"+textStatus);
186
-
187
- alert("errorThrown:"+errorThrown);
188
-
189
- }
190
-
191
- });
231
+ ```
232
+
233
+ ```PHP
234
+
235
+ <?php
236
+
237
+
238
+
239
+ <?php
240
+
241
+
242
+
243
+ header("Content-type: application/json; charset=utf-8");
244
+
245
+ if(isset($_POST['request'])){
246
+
247
+ echo json_encode("OK");
192
248
 
193
249
  }
194
250
 
195
-
196
-
197
- </script>
198
-
199
- </head>
200
-
201
- <body>
251
+ else{
202
-
203
-
204
-
252
+
205
- <div style="text-align: center" class="check">
253
+ echo 'The parameter of "request" is not found.';
206
-
207
-
208
-
209
- <!-- 取得したテキストの表示 --><br />
254
+
210
-
211
- <output></output><br /><br />
212
-
213
-
214
-
215
- <!-- チェックボックスの表示 -->
255
+ }
216
-
256
+
257
+
258
+
217
- <ons-list>
259
+ ?>
218
-
219
-
220
-
221
- <ons-list-header>CheckBox</ons-list-header>
222
-
223
-
224
-
225
- <ons-list-item modifier="tappable">
226
-
227
- <li id="hoge"></li>
228
-
229
- </ons-list-item>
230
-
231
-
232
-
233
- </ons-list>
234
-
235
-
236
-
237
- <!-- ボタン表示 -->
238
-
239
- <ons-button onclick="chkvalue()">Click to Check</ons-button>
240
-
241
- </div>
242
-
243
-
244
-
245
- </ons-page>
246
-
247
- </ons-navigator>
248
-
249
- </div>
250
-
251
- </ons-sliding-menu>
252
-
253
-
254
-
255
- </body>
256
-
257
- </html>
258
260
 
259
261
  ```
260
262
 
261
- ```PHP
262
-
263
- <?php
264
-
265
-
266
-
267
- <?php
268
-
269
-
270
-
271
- header("Content-type: application/json; charset=utf-8");
272
-
273
- if(isset($_POST['request'])){
274
-
275
- echo "OK";
276
-
277
- }
278
-
279
- else{
280
-
281
- echo 'The parameter of "request" is not found.';
282
-
283
- }
284
-
285
-
286
-
287
- ?>
288
-
289
- ```
290
-
291
263
 
292
264
 
293
265
  ###補足情報(言語/FW/ツール等のバージョンなど)
266
+
267
+
268
+
269
+ liplyさんのおっしゃったとおりチェックしていった結果クロスドメイン制約に引っかかっていたためサーバーと通信できていませんでした。
270
+
271
+ なので新しくサーバーを都合したところXMLHttpRequest.status:200となり、通信することができました。
272
+
273
+
274
+
275
+ ですがtextStatusとerrorThrownがエラーになってしまい、textStatusのほうは自力で解決できたのですが、errorThrownのほうが解決できません。
276
+
277
+ もう一度お力をお貸ししていただけないでしょうか・・・
278
+
279
+ よろしくお願いします。
280
+
281
+ errorThrown:SyntaxError:Unexpected token O

2

エラーを変更しました

2016/03/04 05:13

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -16,10 +16,40 @@
16
16
 
17
17
  XMLHttpRequest.status:404
18
18
 
19
+
20
+
19
- XMLHttpRequest.responseText:undefined
21
+ XMLHttpRequest.responseText:<html>
22
+
23
+ <head><tittle>404 Not Found</title></head>
24
+
25
+ <body bgcolor="white">
26
+
27
+ <center><h1>404 Not Found</h1></center>
28
+
29
+ <hr><center>nginx</center>
30
+
31
+ </body>
32
+
33
+ </html>
34
+
35
+ <!--a padding to disable MSIE and Chrome friendly error page -->
36
+
37
+ <!--a padding to disable MSIE and Chrome friendly error page -->
38
+
39
+ <!--a padding to disable MSIE and Chrome friendly error page -->
40
+
41
+ <!--a padding to disable MSIE and Chrome friendly error page -->
42
+
43
+ <!--a padding to disable MSIE and Chrome friendly error page -->
44
+
45
+ <!--a padding to disable MSIE and Chrome friendly error page -->
46
+
47
+
20
48
 
21
49
  textStatus:error
22
50
 
51
+
52
+
23
53
  errorThrown:OK
24
54
 
25
55
  ###ソースコード
@@ -130,7 +160,7 @@
130
160
 
131
161
  type:"post",
132
162
 
133
- url:"http://localhost/php/sample/send.php",
163
+ url:"/php/sample/send.php",
134
164
 
135
165
  data:JSON.stringify(data),
136
166
 
@@ -234,29 +264,23 @@
234
264
 
235
265
 
236
266
 
237
- $request = isset($_SERVER['HTTP_X_REQUESTED_WHIT']) ? strtolower($_SERVER['HTTP_X_REQUESTED_WHIT']): '';
238
-
239
- if($request !== 'xmlhttprequest') exit;
240
-
241
-
242
-
243
- $json = file_get_contents('php://input');
244
-
245
- $data = json_decode($json, true);
267
+ <?php
246
-
247
-
248
-
249
- $text = isset($data['text']) ? $data['text']: "";
268
+
250
-
251
- $callback = isset($_GET['callback']) ? $_GET['callback']: "";
269
+
252
-
253
- $callback = htmlspecialchars(script_tags($callback));
270
+
254
-
255
-
256
-
257
- header('Content-type: text/javascript; charset=utf-8');
271
+ header("Content-type: application/json; charset=utf-8");
272
+
258
-
273
+ if(isset($_POST['request'])){
274
+
275
+ echo "OK";
276
+
277
+ }
278
+
279
+ else{
280
+
259
- printf("{$callback(%s)}", json_encode($data));
281
+ echo 'The parameter of "request" is not found.';
282
+
283
+ }
260
284
 
261
285
 
262
286
 

1

errorメッセージを変更しました!

2016/03/03 08:28

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -14,7 +14,9 @@
14
14
 
15
15
  ###発生している問題・エラーメッセージ
16
16
 
17
- XMLHttpRequest:[object Object]
17
+ XMLHttpRequest.status:404
18
+
19
+ XMLHttpRequest.responseText:undefined
18
20
 
19
21
  textStatus:error
20
22
 
@@ -146,7 +148,9 @@
146
148
 
147
149
  error: function(XMLHttpRequest, textStatus, errorThrown){
148
150
 
149
- alert("XMLHttpRequest:"+XMLHttpRequest);
151
+ alert("XMLHttpRequest.status:"+XMLHttpRequest.status);
152
+
153
+ alert("XMLHttpRequest.responseText:"+XMLHttpRequest.responseText);
150
154
 
151
155
  alert("textStatus:"+textStatus);
152
156