[英]codeigniter not returning json/ajax response
我已經閱讀了很多關於 codeIgniter ajax 響應的內容。 我已經多次修改了我的 ajax 腳本,但 codeIgniter 沒有返回 json 響應,盡管我在 web 控制台的網絡選項卡下使用 firefox 開發者瀏覽器進行調試時看到了響應。 這是我寫的
AJAX 腳本
$("#class_id").change(function(){
var class_id = $("#class_id").val();
alert(class_id);
alert("<?php echo base_url(); ?>teacher/index.php?teacher/set_class_id");
$.ajax({
method: "POST",
url: "<?php echo base_url(); ?>teacher/index.php?teacher/set_class_id",
dataType: 'json',
data: {class_id: class_id},
success: function(response) {
$("#res").html(reponse.class_id);
}
});
return false;
});
控制器
function set_class_id()
{
if ($this->session->userdata('teacher_login') != 1)
redirect(base_url(), 'refresh');
//echo "dsdsd".$this->input->POST('class_id');
if (!empty($this->input->POST('class_id')))
{
$page_data = array('class_id' => $this->input->POST('class_id'));
//$response["JSON"] = json_encode($page_data);
$response = array('class_id' => $this->input->POST('class_id'));
echo json_encode($page_data);
$this->load->view('backend/index', $page_data);
}
}
你有沒有試過使用
$this->output
->set_content_type('application/json')
->set_output(json_encode(array('foo' => 'bar')));
另外,檢查輸出類以獲取更多信息Codeigniter 輸出類
於是我想了個辦法。 僅使用 php,我使用提交按鈕將 class_id 發送到控制器,然后檢查頁面上的響應以啟用主題下拉列表。 雖然我還沒有弄清楚為什么 ajax 還不能工作。 謝謝你們的投入
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.