回答編集履歴
1
質問への回答
test
CHANGED
@@ -3,3 +3,173 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
- [ルーティング - 3.9](https://book.cakephp.org/3/ja/development/routing.html#index-5)
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
### 2020.09.03 追記
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
> すみません、jsonファイルを送るのではなく、<script></script>内で生成したjsonデータを送りたいです。
|
14
|
+
|
15
|
+
紛らわしい書き方をしてしまい、申し訳ございません。
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
上記の方法はjsonファイルではなくjsonデータを送信する方法です。サンプルコードを以下抜粋して記載します。[プロジェクト全体のコードはこちらからご確認ください。](https://github.com/KazmaArakaki/Teratail/tree/289129)
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
**config/routes.php**
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
```php
|
28
|
+
|
29
|
+
$routes->prefix('Api', function (RouteBuilder $builder) {
|
30
|
+
|
31
|
+
$builder->setExtensions(['json']);
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
$builder->fallbacks(DashedRoute::class);
|
36
|
+
|
37
|
+
});
|
38
|
+
|
39
|
+
```
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
**templates/Pages/home.php**
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
```php
|
48
|
+
|
49
|
+
<script>
|
50
|
+
|
51
|
+
window.addEventListener("DOMContentLoaded", async (event) => {
|
52
|
+
|
53
|
+
const response = await fetch("<?= $this->Url->build([
|
54
|
+
|
55
|
+
'prefix' => 'Api',
|
56
|
+
|
57
|
+
'controller' => 'Users',
|
58
|
+
|
59
|
+
'action' => 'create',
|
60
|
+
|
61
|
+
'_ext' => 'json',
|
62
|
+
|
63
|
+
]) ?>", {
|
64
|
+
|
65
|
+
method: "post",
|
66
|
+
|
67
|
+
headers: {
|
68
|
+
|
69
|
+
"Content-Type": "application/json",
|
70
|
+
|
71
|
+
"X-CSRF-TOKEN": "<?= $this->request->getCookie('csrfToken') ?>",
|
72
|
+
|
73
|
+
},
|
74
|
+
|
75
|
+
body: JSON.stringify({
|
76
|
+
|
77
|
+
name: "John Doe",
|
78
|
+
|
79
|
+
}),
|
80
|
+
|
81
|
+
});
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
const responseData = await response.json();
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
console.log({ responseData });
|
90
|
+
|
91
|
+
});
|
92
|
+
|
93
|
+
</script>
|
94
|
+
|
95
|
+
```
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
**src/Controller/Api/UsersController.php**
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
```php
|
104
|
+
|
105
|
+
<?php
|
106
|
+
|
107
|
+
declare(strict_types=1);
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
namespace App\Controller\Api;
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
use Cake\Log\Log;
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
class UsersController extends ApiController {
|
120
|
+
|
121
|
+
public function create() {
|
122
|
+
|
123
|
+
Log::debug(var_export([
|
124
|
+
|
125
|
+
'request_data' => $this->request->getData(),
|
126
|
+
|
127
|
+
], true));
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
$success = true;
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
$this->set(compact([
|
136
|
+
|
137
|
+
'success',
|
138
|
+
|
139
|
+
]));
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
$this->set('_serialize', [
|
144
|
+
|
145
|
+
'success',
|
146
|
+
|
147
|
+
]);
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
```
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
**UsersController::createのログ出力**
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
```plain
|
162
|
+
|
163
|
+
2020-09-03 04:09:39 Debug: array (
|
164
|
+
|
165
|
+
'request_data' =>
|
166
|
+
|
167
|
+
array (
|
168
|
+
|
169
|
+
'name' => 'John Doe',
|
170
|
+
|
171
|
+
),
|
172
|
+
|
173
|
+
)
|
174
|
+
|
175
|
+
```
|