AJAX를 통해 제출할 때 submit_ajax
메서드로 제출하는 양식이 있습니다. 자, AJAX 요청으로 받으면 JSON 객체를 반환하고 싶습니다.MVC (CodeIgniter)에서 AJAX를 사용할 때 반응을 에코하는 올바른 방법
이 경우 두 가지 옵션이 있습니다. MVC 패턴을 따라 무엇을 올바른 방법으로 간주할까요? 제어기
class StackOverflow extends CI_Controller
{
public function submit_ajax()
{
$response['status'] = true;
$response['message'] = 'foobar';
echo json_encode($response);
}
}
옵션 2에서
옵션 1 에코는 제어기로부터 데이터를 수신하고 반향 뷰를 설정한다.
class StackOverflow extends CI_Controller
{
public function submit_ajax()
{
$response['status'] = true;
$response['message'] = 'foobar';
$data['response'] = $response;
$this->load->view('return_json',$data);
}
}
//return_json view
echo json_encode($response);
나는 이것이 당신이 묻고있는 것이 아니라 단지 사이드 노트로서, [Output class] (http://codeigniter.com/user_guide/libraries/output.html)가 편리한 설정 방법을 제공한다는 것을 알고있다. JSON 응답을위한 적절한 MIME 유형 :'$ this-> output-> set_content_type ('application/json') -> set_output (json_encode (array ('foo'=> 'bar')))) ' –