LoginSignup
1
2

More than 3 years have passed since last update.

Cakephp4.x AjaxからのPOSTにアクセスできない対処

Last updated at Posted at 2020-02-27

Cakephp3.xでは下記URLの記事を参考にアクセスできていたのすが、4.xだと空になってしまう。
4.xでは、Json、XMLで入力されたデータにアクセスする場合、オプション機能でそれをデコードが必要でした。

CakePHP + AjaxでのPOSTで受け取った情報が空になる原因
https://blog.supersonico.info/?p=3172

Cakephp 3.x

cakephp3.x&sample.php
//セキュリティコンポーネントを一時無効
if ($this->params['action'] == アクション名) {
    $this->Security->csrfCheck = false;
    $this->Security->validatePost = false;
}
//getDate()で受け取り
$post_data = $this->request->getData();

Cakephp 4.x

cakephp4.x&sample.php
//セキュリティコンポーネントを一時無効
if ($this->params['action'] == アクション名) {
    $this->Security->csrfCheck = false;
    $this->Security->validatePost = false;
}
//input()のオプション機能でデコードし受け取り
$post_data = $this->request->input('json_decode');

参考:
リクエストとレスポンスオブジェクト
PUT、PATCH または DELETE データ
https://book.cakephp.org/4/ja/controllers/request-response.html

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2