gpt4 book ai didi

php - PUT 方法不起作用(CodeIngiter + Codeigniter 休息服务器)

转载 作者:行者123 更新时间:2023-12-02 04:17:05 26 4
gpt4 key购买 nike

我正在使用 Codeigniter 3.01 和 Codeigniter rest server
我在 put 方法中遇到问题。当我通过 put 方法运行 http://codeang.si/api/task/id/2 时(检查附图)并尝试 var_dump($data); 我得到这个“空”结果:

array (size=3)
'task' => boolean false
'active' => boolean false
'date_c' => string '2015-10-11 12:10' (length=16)

这是我的 API Controller :

    <?php
require(APPPATH.'libraries/REST_Controller.php');


class Api extends REST_Controller{

function tasks_get(){
// respond with information about a task
$task = $this->tm->get_all();
if($task){
$this->response($task, 200);
}else{
$this->response(NULL, 404);
}
}

function task_get(){
// respond with information about a task
if(!$this->get('id')){
$this->response(NULL, 400);
}
$task = $this->tm->get($this->get('id'));
if($task){
$this->response($task, 200);
}else{
$this->response(NULL, 404);
}
}

function task_post(){
// create task
$data = array(
'task' => $this->post('task'),
'active' => $this->post('active'),
'date_c' => date("Y-m-d h:i:s")
);
$result = $this->tm->create( $data);
if($result === FALSE){
$this->response(array('status' => 'failed'));
}else{
$this->response(array('status' => 'success'));
}
}

function task_put(){
// update task
$id = $this->get('id');
$data = array(
'task' => $this->put('task'),
'active' => $this->put('active'),
'date_u' => date("Y-m-d h:i:s")
);
var_dump($data, $id);
die();
$result = $this->tm->update($this->get('id'), $data);
if($result === FALSE){
$this->response(array('status' => 'failed'));
}else{
$this->response(array('status' => 'success'));
}
}

function task_delete(){
//delete a task and respond with a status/errors
$result = $this->tm->delete($this->get('id'));
if($result === FALSE){
$this->response(array('status' => 'failed'));
}else{
$this->response(array('status' => 'success'));
}
}

}

这是我的请求结果: Result

希望有人能帮忙。预先感谢您

最佳答案

我找到了解决方案。这不是代码的问题,而是我的“ body ”的问题。我必须在 Postman chrome 扩展中从 form-data 切换到 x-www-form-urlencoded,现在 put 方法工作正常。

这是示例图片: enter image description here

关于php - PUT 方法不起作用(CodeIngiter + Codeigniter 休息服务器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33064048/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com