【问题标题】:how to receieve json array in codeigneetr 4如何在codeigniter 4中接收json数组
【发布时间】:2022-01-25 14:26:42
【问题描述】:

我正在尝试将 json stringfy 数组从 ajax 发送到 Codeigniter 4 控制器。但我无法在 CodeIgniter 控制器中接收数组。我收到错误

500(内部服务器错误)


这是我的 ajax 代码

let adbtn = document.getElementById("aDDbtn");
adbtn.addEventListener("click", function () {
  const xhtp = new XMLHttpRequest();
  xhtp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
      console.log(this.responseText);
    }
  };
  var test1 = [1, 2, 3, 4];
  xhtp.open("POST", window.location.origin + "/crud/test", false);
  xhtp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  let my_json = JSON.stringify(test1);
  xhtp.send(my_json);
});

这是我的 codeigneter 函数

 public function test()
        {
          $n= $this->request->getPost("my_json");
          $array=json_decode($n);
          var_dump($array);
          exit;
        }

我有 javascript 数组,我将其转换为 jason stringyfy,然后尝试对其进行解码,但失败了。谁能帮帮我吗 ?纠正我哪里错了?我试图自己解决但没有成功。

【问题讨论】:

    标签: json ajax codeigniter-4


    【解决方案1】:

    像我一样处理json请求

     public
        function sendActivateCodeViaSms()
        {$res['data']='soem data';
    
            return $this->response->setJSON($res)
                ->setStatusCode(ResponseInterface::HTTP_OK, 'get data')
                ->setContentType('application/json');
    
    
        }
    
    

    【讨论】:

      猜你喜欢
      • 2014-07-02
      • 2021-09-21
      • 2019-06-22
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多