无法访问控制器中的获取发布数据:Codeigniter

Jus*_*n S 5 codeigniter fetch

我正在我的 codeigniter 项目中使用 fetch 发出发布请求。请求看起来像这样

fetch('myurl/mycontroller', {
    method: 'POST',
    headers: {
        "Content-Type": "application/json"
    },
    body: JSON.stringify({
         testdata: 123,
    })
 }).then((res) => {
    console.log(res);
 }).catch(console.log);
Run Code Online (Sandbox Code Playgroud)

我的控制器如下所示

class MyController extends CI_Controller
{
    public function mycontroller()
    {
        $data = $this->input->post('testdata');
        return $data . " is the passed data.";
    }
}
Run Code Online (Sandbox Code Playgroud)

但数据没有传递到我的控制器。我回显了它$_POST,它给了我一个空数组。知道我做错了什么吗?我正在使用 codeigniter 2 (我知道它现在已经很旧了)

Jus*_*n S 2

所以不完全确定实际原因,但 codeigniter 的 CI 核心可能存在一些错误,它不会使用 fetch 解析传递给控制器​​的数据。使用FormData()并且axios我能够解决这个问题。

 var postData = new FormData();
 postData.append('testdata', 123);
 axios.post('myurl/mycontroller', postData).then(function(response){
     console.log("success:", response);
 }).catch(function(error){
     console.log("error:", error);
 });
Run Code Online (Sandbox Code Playgroud)