I have a form that is submitted to the method submit_ajaxwhen submitted via AJAX. Now that I get it as an AJAX request, I want to return a JSON object.
In this case, I have two options. What could be considered the right way, following the MVC pattern?
Option 1
Echo from the controller
class StackOverflow extends CI_Controller
{
public function submit_ajax()
{
$response['status'] = true;
$response['message'] = 'foobar';
echo json_encode($response);
}
}
Option 2 Configure a view that receives data from the controller and echoes.
class StackOverflow extends CI_Controller
{
public function submit_ajax()
{
$response['status'] = true;
$response['message'] = 'foobar';
$data['response'] = $response;
$this->load->view('return_json',$data);
}
}
echo json_encode($response);
source
share