如何在CodeIgniter中使用AJAX从数据库中获取数据?

我想知道如何在CodeIgniter中使用AJAX从数据库中获取数据。 您可以查看下面的代码,找出问题的原因吗? 单击我视图中的链接时没有任何反应。

这是我的观点:

 

这是我的控制器:

 public function get_faq_data() { $this->load->model("model_faq"); $title = $_POST['title']; $data["results"] = $this->model_faq->did_get_faq_data($title); echo json_encode($data["results"]); } 

这是我的模型:

 public function did_get_faq_data($title) { $this->db->select('*'); $this->db->from('faq'); $this->db->where('faq_title', $title); $query = $this->db->get('faq'); if ($query->num_rows() > 0) { return $query->result(); } else { return false; } } 

这是我的JavaScript文件:

 $(".faq_title").click(function() { var title = $(this).text(); $.ajax({ url: 'faq/get_faq_data', data: ({ title: title }), dataType: 'json', type: 'post', success: function(data) { response = jQuery.parseJSON(data); console.log(response); } }); }); 

试试这个:

 $(function(){ // start of doc ready. $(".faq_title").click(function(e){ e.preventDefault(); // stops the jump when an anchor clicked. var title = $(this).text(); // anchors do have text not values. $.ajax({ url: 'faq/get_faq_data', data: {'title': title}, // change this to send js object type: "post", success: function(data){ //document.write(data); just do not use document.write console.log(data); } }); }); }); // end of doc ready 

我看到的问题是这个var title = $(this).val(); 作为你的选择器$(".faq_title")是一个锚,锚点的文字不是值。 所以我建议你使用.text()而不是.val()