CodeIgniter + jQuery UI autocomplete = 500 internal server error (with code) due to CSRF set to TRUE
Here is the view code:
<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>
and the controller code:
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'] = 'f开发者_StackOverflow中文版alse';
$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)
and on CI error logs there seems to be no issues (log_threshold is 1, /logs is chmod 777).
BTW I have my config.php with query_strings TRUE and allow_get_array TRUE.
Any ideas how to fix this issue?
This is almost certainly a CSRF token issue.
See this in the CI forums and this blog post
Nothing in your question suggests that you need to turn query_strings
on or allow_get_array
Try this
comment out this line
$search = $this->input->post('term');
then add $search
to your function as the first argument
public function ac2($search)
Then just try to hit the URL with a browser
http://yourdomain.com/index.php/test/ac2/<insert your search string here>
Now that we know your url is good
change your controller back.
try this...
data: 'term='+req, //<-- change to this
seems you are missing sendig the csrf token with the POST data, try:
$("#input").autocomplete({
source: function(req, add){
var cct = $("input[name=ci_csrf_token]").val(); // <---
$.ajax({
url: '<?php echo base_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);
}
});
});
you can also find the token like:
csrf_test_name:$("input[name=csrf_test_name]").val(),
that token is generated in the view when using the form helper and opening it like:
<?php echo form_open();?>
sources: * http://codeigniter.com/forums/viewthread/176318/ ** own headache
Check your PHP code without the AJAX. Your error suggests that your PHP is the one that is causing the error.
One more thing it is quite better to see if you have results before you call ->result()
精彩评论