CodeIgniter Forums
failure in json response in codeigniter 3 controller method - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: failure in json response in codeigniter 3 controller method (/showthread.php?tid=92120)



failure in json response in codeigniter 3 controller method - solmazbabakan - 12-05-2024

in my codeigniter view page i have a form submission process; 
and in controller method i use following to process form imput data:


PHP Code:
$FORM_KURALLARI PROFIL_OLUSTURMA_FORM_KURALLARI();
$this->form_validation->set_rules($FORM_KURALLARI);
if (
$this->form_validation->run() == FALSE) {
    log_message('debug','unvalid form=='.json_encode(validation_errors()));;
    $response = array(
        'durum' => "warning",
        'icon' => 'fa-times',
        'mesaj' => validation_errors(),
        'buton1' => ceviri('Tamam'true)
    );
    $this->output->set_content_type('application/json')->set_output(json_encode($response));
    return;
}
  

so if data input is not meet conditions as i defined for fields the failure for fields will be displayed;
e.g. input for email address or telephone number!
here while the errors could be logged successfully, json response does not displayed at all;

what cause this problem? and how could i resolve that?


RE: failure in json response in codeigniter 3 controller method - deepakkrr - 04-09-2025

It seems you're encountering an issue where form validation errors are logged but not returned as JSON responses in your CodeIgniter controller. try these few steps to troubleshoot

1. Check for Prior Output - Ensure no output (like whitespace or HTML) is sent before the JSON response. Any prior output can prevent the JSON from being sent correctly.

2. Set Content Type and Output Correctly - Use CodeIgniter's output class to set the content type and output the JSON response:

Code:
php
Copy
Edit
$this->output
    ->set_content_type('application/json')
    ->set_output(json_encode($response));


3. Use exit After Outputting JSON - After sending the JSON response, call exit; to prevent further script execution:

Code:
php
Copy
Edit
$this->output
    ->set_content_type('application/json')
    ->set_output(json_encode($response));
exit;

4. Inspect Network Response - Use browser developer tools to inspect the network response and ensure the JSON is being sent as expected.