我想在控制器中以json
格式顯示響應,並且不想移動視圖。所以,請讓我以任何方式讓我可以顯示json
迴應。如何在codeigniter控制器中定義json頭文件?
這裏是我想要添加的代碼json
標題。
$result = $this->mod_doc->get_list();
echo json_encode($result);
我想在控制器中以json
格式顯示響應,並且不想移動視圖。所以,請讓我以任何方式讓我可以顯示json
迴應。如何在codeigniter控制器中定義json頭文件?
這裏是我想要添加的代碼json
標題。
$result = $this->mod_doc->get_list();
echo json_encode($result);
只要給這些試一試。
public function signin() {
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
//add the header here
header('Content-Type: application/json');
echo json_encode($arr);
}
或嘗試這種方式
return $this->output
->set_content_type('application/json')
->set_status_header(500)
->set_output(json_encode(array(your array)));
$result = $this->mod_doc->get_list();
$this->output
->set_content_type('application/json') //set Json header
->set_output(json_encode($result));// make output json encoded
更多參考https://ellislab.com/codeigniter/user-guide/libraries/output.html
來源:https://www.codeigniter.com/user_guide/libraries/output.html?highlight=_output
$response = array('status' => 'OK');
$this->output
->set_status_header(200)
->set_content_type('application/json', 'utf-8')
->set_output(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))
->_display();
exit;
感謝Harigovind。它的工作很好:) – Shalu
@Shalu請接受此作爲答案,如果它爲你工作。以便其他人可以在將來使用它。樂意效勞。 –
only header('Content-Type:application/json'); 爲我工作 – ergunkocak