由于CSRF设置为TRUE,因此CodeIgniter + jQuery UI自动完成= 500个内部服务器错误(带有代码)
问题描述:
这是查看代码:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<!-- Load JQuery UI -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript">
$( function() {
$("#input").autocomplete({
source: function(req, add){
$.ajax({
url: '<?php echo base_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);
}
});
});
</script>
</head>
<?php
echo form_open();
echo form_input('input', '', 'id="input"');
echo form_close();
?>
</html>
和控制器代码:
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);
}
当我在输入框中输入任何内容时,会在控制台上得到此信息:
When I type anything in into the input box I get this on the console:
POST http://my.example.com/test/ac2 500 (Internal Server Error)
并且在CI错误日志上似乎没有问题(log_threshold为1,/logs为chmod 777).
and on CI error logs there seems to be no issues (log_threshold is 1, /logs is chmod 777).
顺便说一句,我的config.php具有query_strings TRUE和allow_get_array TRUE.
BTW I have my config.php with query_strings TRUE and allow_get_array TRUE.
有什么想法可以解决此问题吗?
Any ideas how to fix this issue?
答
这几乎可以肯定是CSRF令牌问题.
This is almost certainly a CSRF token issue.
在CI论坛中参见此和此
See this in the CI forums and this blog post