0

试图为 jQuery UI V10 Autocomplete 建立一个测试平台。我的演示程序正在运行,但是当我将代码改编为使用 C# 和 ASP.NET Web 服务的示例时,我得到一个状态 200 parseerror。json 数据返回时用双引号括起来。当我将响应放入 jsonLint 时,它会在我删除外引号时进行验证。它也有“d”。我期望从 ASP Web 服务获得的前缀。

如何获得返回有效 JSON 的响应?

jQuery代码:

$(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(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;
    }
}

从 web 服务调用返回的 xhr 对象:

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"
4

1 回答 1

1

是什么AutoSuggestReturn.ConvertToJson

自动完成正在寻找某种类型的 json 数组,您应该能够在List<AutoSuggestReturn> matches不经过字符串转换的情况下返回您的枚举。序列化将为您处理。

于 2013-06-19T16:36:11.900 回答