CodeIgniter Forums
JSON return string ajax - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: JSON return string ajax (/showthread.php?tid=78516)



JSON return string ajax - ferb - 02-01-2021

I send form by post, and response will be a json encode

this return controller


$msg = ["tipo" => 'danger',
"icon" => 'fa fa-times',
"msg" => 'Email no encontrado.'];

return $this->response->setJSON($msg);


but retrivied and string


i using codeigniter 4.1.1


RE: JSON return string ajax - demyr - 02-02-2021

You need Json stringify. Please look for it on internet

An example :

PHP Code:
$('#send-button').click(function(){
     $.each($('#holder').serializeArray(), function() {
    answer[this.name] = this.value;
    
});
             
var answers JSON.stringify(answernull2);
      $.ajax({
          url "<?php echo base_url('your-controller-and-method');?>",
          method"POST",

etc