【问题标题】:jQuery Autocomplete widget gets ParseError when calling C# webservice调用 C# webservice 时 jQuery Autocomplete 小部件获取 ParseError
【发布时间】:2013-06-15 05:54:00
【问题描述】:

试图获得一个适用于 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(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"

【问题讨论】:

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


    【解决方案1】:

    AutoSuggestReturn.ConvertToJson 是什么?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      • 2020-04-19
      • 2017-07-06
      • 2020-07-31
      • 1970-01-01
      • 1970-01-01
      • 2011-01-05
      相关资源
      最近更新 更多