i am trying to send json stringfy array from ajax to Codeigniter 4 controller. but i am not able to receive array in CodeIgniter controller. I am getting error
500 (Internal Server Error)
here is my ajax code
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);
});
and here is my codeigneter function
public function test()
{
$n= $this->request->getPost("my_json");
$array=json_decode($n);
var_dump($array);
exit;
}
i had javascript array , i converted this to jason stringyfy then tried to decode that but am failed. can anyone please help me ? correct me where am wrong ? i tried to solve bymyself but not succeed.
do it like me for json request
public
function sendActivateCodeViaSms()
{$res['data']='soem data';
return $this->response->setJSON($res)
->setStatusCode(ResponseInterface::HTTP_OK, 'get data')
->setContentType('application/json');
}