I am trying to implement jQuery Autocomplete in my Codeigniter project, but I get the Following error in Firefox.
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
JavaScript
jQuery("#search").autocomplete({
source: function(req, response) {
$.ajax({
url: "<?php echo site_url('C_systemUser/search_value'); ?>",
dataType: "text",
success: function(data) {
var re = $.ui.autocomplete.escapeRegex(req.term);
var matcher = new RegExp("^" + re, "i");
response($.grep(data, function(item) {
return matcher.test(item.label);
}));
}
});
},
minLength: 2,
select: function(event, ui) {
event.preventDefault();
$('#search').val(ui.item.label);
$('#id').val(ui.item.value);
}
});
Modal
function systemUserSearch($q) {
$this - > db - > select('systemUserID,systemUserName');
$this - > db - > like('systemUserName', $q);
$query = $this - > db - > get('systemuser');
if ($query - > num_rows() > 0) {
foreach($query - > result_array() as $row) {
$result_row['label'] = htmlentities(stripslashes($row['systemUserName']));
$result_row['value'] = htmlentities(stripslashes($row['systemUserID']));
$result[] = $result_row;
}
echo json_encode($result);
}
}
Control
public function search_value()
{
$q=$this->input->get('query');
$this->load->model('m_autocomplete');
$this->m_autocomplete->systemUserSearch($q);
}