【发布时间】:2011-07-18 18:13:04
【问题描述】:
我在使用 ajax 发布函数从控制器返回数据时遇到问题。这里发生的是用户将输入一个样式编号,将该编号提交给控制器。控制器获取提交的值,查询数据库并将结果返回给控制器,然后控制器返回一个数组,然后我将其编码为 json。但我不断收到 500 内部服务器错误???
这是我的控制器
//return schools based on style id
public function search() {
$input = json_decode($this->input->post('obj'));
$style_id = $input['style_id'];
$parent_id = $input['parent_id'];
$data = array();
if ($q = $this->page_model->search_results($style_id, $parent_id)) {
$data = $q;
}
echo json_encode($data);
}
这是我的模型
//get all entries
function search_results($style_id, $parent_id) {
$options = array(
'Style' => $style_id,
'Parent_ID' => $parent_id
);
$this->db->select('*');
$this->db->from('pages');
$this->db->join('entry', 'pages.Page_ID = entry.Parent_Page_ID', 'inner');
$this->db->where($options);
$q = $this->db->get();
if ($q->num_rows() > 0) {
return $q->result();
}
}
这是我的 javascript
//dress style search
$($searchBtn).click(function(event) {
var style_id,
parent_id,
obj = {};
//get the value of the input fields
searchVal = event.currentTarget.previousSibling.value;
parentID = event.currentTarget.parentElement.childNodes[3].value;
//The object to be passed to the controller
obj = { 'style_id' : searchVal, 'parent_id' : parentID };
//POST the values in json notation, return the search results in json notation
$.post(base_url + 'index.php/page/search',
obj,
function(data) {
console.log(data);
},
'json');
return false;
});
base_url=http://localhost:8888/lexsreg
顺便说一句 - 如果我注释掉 return false,我会得到一个从我的控制器回显的 json 字符串。
提前致谢!
【问题讨论】:
标签: javascript jquery ajax json codeigniter