Dilshi
Dilshi

Reputation: 553

cannot fill autocomplete with data

var first = true;
var json = '[';
if (!first) {
    json += ',';
} else {
    first = false;
}
// $.each(data, function(i, elem) {
json += '{label:"kasun"}';


json += ']';
console.log(json);
$("#p_name").autocomplete({
    minLength: 2,
    source: x,
    focus: function (event, ui) {
        $("#p_name").val(ui.item.label);
        return false;
    },
    select: function (event, ui) {
        return false;
    }
})

i tried above code to populate auto complete with data.when i try this i get following error. if u have alternative please let me know

    GET http://localhost/NEW/patient_channel/[%7Blabel:%22kasun%22%7D]?term=ka 404 (Not Found) 

Upvotes: 1

Views: 69

Answers (2)

Vishwanath
Vishwanath

Reputation: 6004

Your source x is not visible elsewhere in the code, I believe its a URL and since it is giving 404 as mentioned by tou, means the resource doesn not exist on server.

Fix the 404 first, after that you have to format the data returned by server in format expected by autocomplete as follows.

[{'label' : dataValue, 'value' : dataValue}]

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

The source should be an array, not a string

You need

var json = [];

// $.each(data, function(i, elem) {
json.push({label:"kasun", value:"kasun"})
//or json.push('kasun')

//});

console.log(json);

jQuery(function ($) {
    $("#p_name").autocomplete({
        minLength: 2,
        source: json,
        focus: function (event, ui) {
            $("#p_name").val(ui.item.label);
            return false;
        },
        select: function (event, ui) {
            return false;
        }
    })
});

Demo: Fiddle or Fiddle

Upvotes: 3

Related Questions