php - CakePHP + Crud "Add"操作 (POST) 返回 412 前提条件失败

标签 php mysql json cakephp crud

CakePHP 3 中的 Add 操作(POST 方法)不起作用,它询问我已经在 json 消息中通知的字段。让我解释一下:

我的场景是什么:

  • 我正在使用 WAMP,我的 Apache 已激活 mod_rewrite
  • 我正在使用 composer 并安装了 CakePHP 3 和 CRUD (friendsofcake/crud);
  • 我正确设置了MySQL数据库连接;
  • 我正在使用 MySQL 示例数据库 sakila
  • 我从 Bake 生成了 Country 模型;
  • 我更改了 AppController 类内容以加载 CRUD 插件,将其内容更改为:
namespace App\Controller;

use Cake\Controller\Controller;

class AppController extends Controller {

    use \Crud\Controller\ControllerTrait;

    public $components = [
        'RequestHandler',
        'Crud.Crud' => [
            'actions' => [
                'Crud.Index',
                'Crud.View',
                'Crud.Add',
                'Crud.Edit',
                'Crud.Delete'
            ],
            'listeners' => [
                'Crud.Api',
                'Crud.ApiPagination'
            ]
        ]
    ];
}
  • 我在 app/config/routes.php 中添加了 Router::extensions(['json', 'xml']);
  • 我添加了 $routes->resources('Country') 而没有删除存在于 app/config/routes.php 中的默认路由;
  • 我的根站点地址是 http://localhost:8080/mtag

问题是如何发生的:

我正在使用 Postman for Chrome并将 header AcceptContent-Type 值设置为 application/json 并将 POST 发送到以下链接:http://localhost:8080/mtag/country具有以下 json 内容:

{
  "country": "BlaBlaBla",
  "last_update": "2015-12-24"
}

但我收到以下状态代码:412 先决条件失败。 在返回的 json 中:

success: false,
data: {
  code: 412,
  message: "2 validation errors occured",
}

指示的缺失字段是 countrylast_update 但我在 json 中通知了他们。我做错了什么?有人举例说明如何使用 json 正确使用 CakePHP CRUD 吗?

最佳答案

当我使用 Postman 测试 cakePHP CRUD 函数时,我不使用 application/json 作为 Content-Type 而是将其留空。对于正文部分,在使用 raw 正文时,只需使用 form-data 而不是 json 对象。

关于php - CakePHP + Crud "Add"操作 (POST) 返回 412 前提条件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34457547/

相关文章:

Python排序方法将两位数字字符串视为三位数字字符串

php - 新关键字与创建实例

php - 无法将 .png ext 保存到 Laravel 数据库中

c# - ASP.NET 数据注释 : How to share with a JSON client?

javascript - 下划线js查找嵌套json

php - 根据用户的兴趣显示出版物

PHPSESSID Cookie 被多次发送

php - Laravel 5.1 后台任务在连接到 Azure Web 作业时查询数据库无法工作

php - 在表的不同字段中插入特定元素

php - 当 POST 字段为空时,将 NULL 插入外键