Reputation: 868
I have jQuery UI autocomplete code as follows :
$("#keyword").autocomplete({
source: function(request, response) {
$.ajax({
url: "<?php echo base_url(); ?>index.php/formhandler/autocomplete",
data: {term: request.term, data: $("#city").val()},
dataType: "json",
success: function(data) {
response($.map(response, function(item) {
return {
label: item,
value: item
}
}));
}
})
}
});
and I have the response as json as follows :
["result1"]["reusult2"]["result3"]["result4"]["result4"]
How can I autocomplete the keyword field using this ?
Upvotes: 0
Views: 301
Reputation: 3077
You don't seem to be using the parameter data of your ajax success function. Open the JavaScript console (F12 in chrome) and check if you get any error.
$("#keyword").autocomplete({
source: function(request, response) {
$.ajax({
url: "<?php echo base_url(); ?>index.php/formhandler/autocomplete",
data: {term: request.term, data: $("#city").val()},
dataType: "json",
success: function(data) {
//check what data contains. it should contain a string like "['val1','val2','val3','val4']"
//the next line should use $.map(data and not $.map(response
response($.map(response, function(item) {
return {
label: item,
value: item
}
}));
}
})
}
});
Upvotes: 1
Reputation: 6000
You need to combine these arrays> Use :
$.merge
You may have to use a loop. A example can be this :
var first = ['a','b','c'];
var second = ['d','e','f'];
$.merge( $.merge([],first), second);
Which would output :
["a","b","c","d","e","f"]
Upvotes: 0
Reputation: 505
Why are you using $.ajax. You can just get the data from $.get as a source.
$("#keyword").autocomplete({
source : function (request, response) {
$.get("<?php echo base_url(); ?>index.php/formhandler/autocomplete", {term: request.term, data: $("#city").val()}, function (data) {
response(data);
});
}
});
Upvotes: 0