0

Trying to get a test bed working for jQuery UI V10 Autocomplete. I have the demo programs working, but when I adapt the code to an example using C# and an ASP.NET webservice, I am getting a status 200 parseerror. The jSon data is coming back wrapped in double quotes. When I put the response in jsonLint, it validates when I remove the outer quotes. It also has the "d." prefix that I expect from an ASP webservice.

How do I get the response to return valid JSON?

jQuery code:

$(settings.selector).autocomplete({
    source: function (request, response) {
        var data = JSON.stringify({
            prefixText: request.term,
            count: settings.count
        });
        ajaxCall(settings.url, data, {
            onSuccess: function (data) {
                var matches = [];
                $.each(data, function (item) {
                    matches.push({
                        label: data[item].label,
                        value: data[item].value,
                        id: data[item].id
                    });
                });
                response(matches);
            }
        });
    },
    minLength: settings.minLength,
    change: function (event, ui) {
        if (ui && ui !== null && ui.item && ui.item !== null) {
            currentValue = ui.item.label;
            if (settings.onChange && settings.onChange !== null) {
                settings.onChange(ui.item, settings.selector, oldValue);
            }
        } else {
            $(settings.selector).val(oldValue);
        }
    },
    select: function (event, ui) {
        currentValue = ui.item.label;
        if (settings.onSelect && settings.onSelect !== null) {
            settings.onSelect(ui.item, settings.selector, oldValue);
        }
    },
    open: function () {
        $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
    },
    close: function () {
        $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
    }
});

WebService Code:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class TestAutocomplete : System.Web.Services.WebService
{

    [WebMethod(BufferResponse = true, Description = "Lookup City")]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string CityList(string prefixText, int count)
    {
        List<AutoSuggestReturn> matches = AssessorBusinessLayer.City.List(prefixText, count);
        Messages.ConsoleLog(string.Format("Prefix: {0} return {1} matches", prefixText, matches.Count));
        string ret = AutoSuggestReturn.ConvertToJson(matches);
        return ret;
    }
}

xhr object on return from webservice call:

xhr: Object
abort: function ( statusText ) {
always: function () {
complete: function () {
done: function () {
error: function () {
fail: function () {
getAllResponseHeaders: function () {
getResponseHeader: function ( key ) {
overrideMimeType: function ( type ) {
pipe: function ( /* fnDone, fnFail, fnProgress */ ) {
progress: function () {
promise: function ( obj ) {
readyState: 4
responseText: "{"d":"[{\"id\":\"10499\",\"label\":\"Delmar,AL (35551)\",\"value\":\"Delmar,AL (35551)\"},{\"id\":\"2679\",\"label\":\"Delmar,DE (19940)\",\"value\":\"Delmar,DE (19940)\"},{\"id\":\"2401\",\"label\":\"Delmar,IA (52037)\",\"value\":\"Delmar,IA (52037)\"},{\"id\":\"2679\",\"label\":\"Delmar,MD (21875)\",\"value\":\"Delmar,MD (21875)\"},{\"id\":\"3584\",\"label\":\"Delmar,NY (12054)\",\"value\":\"Delmar,NY (12054)\"},{\"id\":\"4780\",\"label\":\"Delmita,TX (78536)\",\"value\":\"Delmita,TX (78536)\"},{\"id\":\"3352\",\"label\":\"Delmont,NJ (08314)\",\"value\":\"Delmont,NJ (08314)\"},{\"id\":\"11550\",\"label\":\"Delmont,PA (15626)\",\"value\":\"Delmont,PA (15626)\"},{\"id\":\"4574\",\"label\":\"Delmont,SD (57330)\",\"value\":\"Delmont,SD (57330)\"}]"}"
setRequestHeader: function ( name, value ) {
state: function () {
status: 200
statusCode: function ( map ) {
statusText: "OK"

1 Answer 1

1

What is AutoSuggestReturn.ConvertToJson ?

Auto complete is looking for some type of json array and you should be able to return your List<AutoSuggestReturn> matches enumeration without going through a string conversion. Serialization will be handled for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.