【问题标题】:Why I can't get $this->post() value from JSON.stringify POST data为什么我无法从 JSON.stringify POST 数据中获取 $this->post() 值
【发布时间】:2020-04-02 05:06:39
【问题描述】:

我在 CodeIgniter 框架中创建了一个简单的 RESTful Web 服务作为我的学生的示例项目。

我的 API 函数之一出现问题,如下所述:

  public function newmember_post()
  {
    $data = json_decode($this->post()[0], true);     // <-- point of my problem

    $ok = $this->guests_model->insertMember($data);

    if( $ok ) {
      $this->response( ['status'=>'OK', 'member_ID'=> $ok ], 200 );
    } else {
      $this->response( ['status'=>'Failed'], 500 );
    }
  }

当我尝试获取类似的 POST 数据项时

  $nama = $this->post('nama);
  $alamat = $this->post('alamat');
  ...

所有数据项都有null 值。

当我尝试 var_dump( $this-&gt;post() ) 时,我得到了这个:

array(1) {
  [0]=>
  string(139) "{"nama":"Devi Chung","alamat":"Jl. Baji Ateka 20","kota":"Makassar","negara":"ID","kodepos":"90131","telepon":"871555","hp":"081234567890"}"
}

// it's a fake identity, no problem

POST 数据变成一个数组,所以,我获取 POST 数据的方案是这样的

$data = json_decode($this->post()[0], true);

我的工作出了什么问题,所以我无法使用 $this-&gt;post() 以正常方式获取 POST 数据


在前端,我使用 JavaScript Promise 来做 API 请求数据 POST

const endpoint_url = 'http://my.services.web/api';

function newMemberSave(){
  var new_member = {
    'nama': document.getElementById("nama").value,
    'alamat': document.getElementById("alamat").value,
    'kota': document.getElementById("kota").value,
    'negara': document.getElementById("negara").value,
    'kodepos': document.getElementById("kodepos").value,
    'telepon': document.getElementById("telepon").value,
    'hp': document.getElementById("handphone").value
  }

  fetch( endpoint_url + "/guests/newmember", {
    method: 'POST',
    body: JSON.stringify(new_member)      // <-- point of my problem
  } )
    .then(status)
    .then(json)
    .then(function(data) {
      console.log(data);
      if( data.status == 'OK' ){
        alert( "New member saved");
      } else {
        alert( "Save data failed, try again");
      }
    })
    .catch(error);
}

为了让我更容易处理 API 响应,我制作了这些函数

function status(response) {
  if (response.status !== 200) {
    console.log("Error : " + response.status);
    return Promise.reject(new Error(response.statusText));
  } else {
    return Promise.resolve(response);
  }
}

function json(response) {
  return response.json();
}

function error(error) {
  console.log("Error : " + error);
}

如果我不使用JSON.stringify,POST 数据在后端变为空。

如果我添加 Content-Type: application/json 标头,我会收到错误 405(不允许的方法)

【问题讨论】:

    标签: json rest codeigniter post stringify


    【解决方案1】:

    你需要在中间写“输入”,试试下面

        $nama = $this->input->post('nama);
        $alamat = $this->input->post('alamat');
    

    【讨论】:

    • CI_Controller 没错,我们使用$this-&gt;input-&gt;post()$this-&gt;input-&gt;get(),但REST_Controller 我们只使用$this-&gt;post()$this-&gt;get()。我也试过了,得到了null发布数据
    • $this->input->post() 或 $_POST 有什么问题?
    猜你喜欢
    • 2017-08-31
    • 2015-10-22
    • 2011-05-21
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多