CodeIgniter + jQuery UI autocomplete = 500内部服务器错误(带代码)
这是视图代码:
$( function() { $("#input").autocomplete({ source: function(req, add){ $.ajax({ url: 'test/ac2', dataType: 'json', type: 'POST', //data: req, data: 'input='+req, success: function(data){ if(data.response =='true'){ add(data.message); } } }); }, minLength: 2, select: function(event, ui){ $(this).end().val(ui.item.value); } }); });
和控制器代码:
class Test extends CI_Controller { function index() { $this->load->view('vw/test_vw'); } public function ac2() { //$search = $this->input->post('term'); $search = $this->input->post('input'); $data['response'] = 'false'; $this->db->select('*'); $this->db->from('loc_exercise'); $this->db->like('locations', $search); $locations = $this->db->get()->result(); if (count($locations) > 0) { $data['message'] = array(); foreach ($locations as $location) { $data['message'][] = array( 'label' => $location->locations, 'item' => $location->locations, 'value' => $location->locations ); } $data['response'] = 'true'; } echo json_encode($data); }
当我在输入框中输入任何内容时,我会在控制台上输入:
POST http://my.example.com/test/ac2 500 (Internal Server Error)
在CI错误日志上似乎没有问题(log_threshold是1,/ logs是chmod 777)。
顺便说一句,我有我的config.php,query_strings为TRUE,allow_get_array为TRUE。
有任何想法如何解决这个问题?
这几乎肯定是CSRF令牌问题。
在CI论坛和此博客文章中查看此内容
您的问题中没有任何内容表明您需要打开allow_get_array
或allow_get_array
试试这个
注释掉这一行
$search = $this->input->post('term');
然后将$search
作为第一个参数添加到您的函数中
public function ac2($search)
然后尝试使用浏览器点击URL
http://yourdomain.com/index.php/test/ac2/
现在我们知道你的url很好了
改变你的控制器。
试试这个…
data: 'term='+req, //<-- change to this
似乎你缺少使用POST数据发送csrf令牌,尝试:
$("#input").autocomplete({ source: function(req, add){ var cct = $("input[name=ci_csrf_token]").val(); // <--- $.ajax({ url: 'test/ac2', dataType: 'json', type: 'POST', //data: req, data: 'input='+req, 'ci_csrf_token': cct, // <--- success: function(data){ if(data.response =='true'){ add(data.message); } } }); }, minLength: 2, select: function(event, ui){ $(this).end().val(ui.item.value); } }); });
你也可以找到像这样的令牌:
csrf_test_name:$("input[name=csrf_test_name]").val(),
当使用表单助手并打开它时,该视图在视图中生成:
来源:* http://codeigniter.com/forums/viewthread/176318/ **自己头疼
在没有AJAX的情况下检查您的PHP代码。 您的错误表明您的PHP是导致错误的PHP。
还有一件事是,在调用之前看看你是否有结果会更好->result()