调用C#webservice时,jQuery Autocomplete小部件获取ParseError

时间:2013-06-18 20:51:23

标签: jquery asp.net json web-services autocomplete

尝试使用jQuery UI V10自动完成功能的测试台。我有演示程序工作,但当我使用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代码:

[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;
    }
}

从webservice调用返回的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"

1 个答案:

答案 0 :(得分:1)

什么是AutoSuggestReturn.ConvertToJson

自动完成正在查找某种类型的json数组,您应该能够返回List<AutoSuggestReturn> matches枚举,而无需进行字符串转换。序列化将为您处理。