回答編集履歴

2

ちょうせい

2021/07/15 01:34

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -29,3 +29,41 @@
29
29
  fetch("sample.json").then(res=>res.json()).then(console.log);
30
30
 
31
31
  ```
32
+
33
+
34
+
35
+ # jsでjsonをおくってphpで解釈
36
+
37
+ ```javascript
38
+
39
+ const data={
40
+
41
+ "test":"aaaa",
42
+
43
+ "type":{
44
+
45
+ "a": "あああ",
46
+
47
+ "b": "いい"
48
+
49
+ }
50
+
51
+ };
52
+
53
+ const body=new FormData();
54
+
55
+ body.append("json",JSON.stringify(data));
56
+
57
+ fetch("api.php",{method:"post",body}).then(res=>res.text()).then(console.log);
58
+
59
+ ```
60
+
61
+ //api.php
62
+
63
+ ```PHP
64
+
65
+ <?PHP
66
+
67
+ print_r(json_decode($_POST["json"],JSON_OBJECT_AS_ARRAY));
68
+
69
+ ```

1

修正

2021/07/15 01:34

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -1,3 +1,31 @@
1
1
  例示されているjsonが正しいjsonではないので今のままでは
2
2
 
3
3
  パーサーエラーになります
4
+
5
+
6
+
7
+ //sample.json
8
+
9
+ ```json
10
+
11
+ {
12
+
13
+ "test":"aaaa",
14
+
15
+ "type":{
16
+
17
+ "a": "あああ",
18
+
19
+ "b": "いい"
20
+
21
+ }
22
+
23
+ }
24
+
25
+ ```
26
+
27
+ ```javascript
28
+
29
+ fetch("sample.json").then(res=>res.json()).then(console.log);
30
+
31
+ ```