gpt4 book ai didi

javascript - 使用 Codeigniter 发布 JSON 返回 500 错误

转载 作者:行者123 更新时间:2023-11-30 06:03:31 25 4
gpt4 key购买 nike

我在使用 ajax post 函数从我的 Controller 返回数据时遇到了问题。这里发生的是用户将输入一个样式编号,将该编号提交给 Controller 。 Controller 获取提交的值,查询数据库并将结果返回给 Controller ,然后 Controller 返回一个数组,然后我将其编码为 json。但是我一直收到 500 内部服务器错误???

这是我的 Controller

//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,我确实会收到从我的 Controller 回显的 json 字符串。

提前致谢!

最佳答案

您必须包含 csrf token 。如这两篇博客中所述,有几种方法可以做到这一点。

http://aymsystems.com/ajax-csrf-protection-codeigniter-20

http://codeigniter.com/forums/viewthread/176318/

关于javascript - 使用 Codeigniter 发布 JSON 返回 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6737365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com