Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Autocomplete won't load remote values

Okay, so JQuery's autocomplete widget is driving me nutz!
I have tried numerous ways of loading the widget. I am currently getting the following:

Error: jQuery15105511000803127266_1353087819681 was not called - parsererror

and the Response value (from firebug) appears to be System.string[] though I'm not sure if it's an a string who's value is System.string[] or an actual system.string[] object.

Am I just being stupid, or am I missing something (please be kind in your answer to that last question...)?

My javascript is:

$("#clientName").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "/supplier/apSupplierSearch/",
            data: { searchAPName: clientName.value },
            dataType: "json",
            type: "POST",
            success: function (data) {
                //response(data);
                response($.map(data, function (item) {
                    return {
                        label: item.Name,
                        value: item.Name
                    }
                }))
            }
        }); // ajax
    }, // function [{
    scroll: true,
    scrollHeight: 600,
    minLength: 4
});

My WebMethod is:

[WebMethod]
public string[] apSupplierSearch(string searchAPName)
{
    IList<int> selectedPropertyIDs = new List<int>();
    string currentRole = UserServices.GetCurrentRole();
    Property currentProperty = UserServices.GetCurrentPropety();
    List<ApSupplier> suppliers = ApSupplierQueries.GetApSuppliers(searchAPName, selectedPropertyIDs, currentRole, currentProperty);
    List<string> supplierList = new List<string>();
    foreach (ApSupplier supplier in suppliers)
    {
        supplierList.Add(supplier.Name);
    }
    return supplierList.ToArray();
}
like image 529
SnagQueensHubby Avatar asked Jul 22 '26 11:07

SnagQueensHubby


1 Answers

I'm not too familiar with C# but you probably want to print the supplier list rather than return it. When doing AJAX, you acutally have to output data, not just return it from a method (but that could be my misunderstanding of the language).

Secondly, you need to use a library to create a JSON string from the array created from toArray(). Otherwise, jQuery doesn't recognize the response as JSON and won't parse it.

like image 169
Adam Avatar answered Jul 24 '26 02:07

Adam