parsererror for auto-complete search script
i am writing an auto-complete function for a search box on my website and am facing a parsererror when i make an ajax request. i have a feeling it has to do with parsing the json. i am using the json_encode method in php, so i dont think there should be any trouble - regardless, would appreciate if you could help me debug --
code:
/* php script that is called to make a json array of possible search terms */
-
<?php
if (strlen($_REQUEST['search-text']) < 1) {
print '[{}]';
exit;
}
$terms = array(
'access',
'action',
'accumulate',
'boston',
'byte',
'xaml',
'xoops',
);
$possibilities = array();
foreach ($terms as $term) {
if (strpos($term, strtolower($_REQUEST['search-text'])) === 0) {
$possibilities[] = $term;
}
}
print json_encode($possibilities);
?>
SAMPLE OUTPUT for search-text = 'ac' is ["access","action","accumulate"]
/* javascript code that makes the ajax request */
-
// suggestion list upon query
var $autocomplete = $('<ul class="autocomplete"></ul>').hide().insertAfter('#search-text');
$('#search-text').attr('autocomplete','off');
$('#search-text').keyup(function() {
$.ajax({
'url': 'jquery_search_autocomplete.php',
'data': {'search-text': $('#search-text').val()},
'dataType': 'json',
'type': 'POST',
'error': function (XMLHttpRequest, textStatus, errorThrown) {
$('<p></p>').text(textStatus).insertAfter('#search-text');
},
'success': function(data) {
if (data.length) {
$autocomplete.empty();
$.each(data, function(index, term) {
$('<li></li>').text(term).appendTo($autocomplete).click(function() {
$('#search-text').val(term);
$autocomplete.hide();
});
});
$autocomplete.show();
}
}
});
});
again, an error is being thrown upon the ajax request - errortext = parsererror
thanks in advance!