【发布时间】: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