doulan9287 2013-05-25 02:02
浏览 37
已采纳

CakePHP REST Put / Post不接受数据

I am attempting to write a RESTful service using CakePHP 2.3.5. So far I've successfully created the GET functions for the resource I'm working with. I can send a GET request to example.com/areas.json or to example.com/areas/1.json and it returns the data in my database.

However, I started trying to get the edit function working. I wrote a simple edit method that simply saved the incoming data from $this->request->data. I'm using Postman to test the functionality and sending raw JSON over PUT or POST to example.com/areas/1.json returns a message telling me that the data couldn't be saved. I made the method send me more information when it failed and it tells me that there is no incoming data in either $this->request->data or $this->data.

I've been searching the Internet for solutions to this or similar problems, but everything I have tried has failed so far. I've attempted disabling CSRF checks, disabling the SecurityComponent altogether, and multiple other fixes all involving the security. Changing any of those resulted in black holing the request.

Does anyone have any thoughts on what else I could try to get CakePHP to accept the JSON data into a request? I'll include my edit function below in case that helps.

public function edit($id)
{
    $this->Area->id = $id;
    $message['request-data'] = $this->request->data;
    if ($this->Area->save($this->request->data)) {
        $message['response'] = $this->Area->findById($id);
    } else {
        $message['response'] = "Error";
    }
    $this->set(array(
        'message' => $message,
        '_serialize' => array('message')
    ));
}
  • 写回答

1条回答 默认 最新

  • dqqy64515 2013-05-25 02:47
    关注

    First, make sure the Content-Type of the request is application/json.

    Second, CakePHP doesn't automatically decode the JSON payload; you have to do it manually. From the manual:

    // Get JSON encoded data submitted to a PUT/POST action
    $data = $this->request->input('json_decode');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改