【问题标题】:Jquery Autocomplete - JSON.Parse errorJquery 自动完成 - JSON.Parse 错误
【发布时间】:2012-07-03 19:46:01
【问题描述】:

我正在尝试在标准 mvc 应用程序中实现此 Web api 自动完成功能。 http://techbrij.com/987/jquery-ui-autocomplete-asp-net-web-api

这是来自 Firebug 的屏幕截图http://sdrv.ms/N0WkHP

我创建了一个控制器方法并添加了 jquery 脚本,但我不断收到“JSON.parse:意外字符”错误。我的数据中没有看到任何异常字符。

$(document).ready(function () {
$('#txtSearch3').autocomplete({
    source: function (request, response) {
        $.ajax({
            url: '/home/Get',
            type: 'GET',
            cache: false,
            data: request,
            dataType: 'json',
            success: function (json) {
                // call autocomplete callback method with results
                response($.map(json, function (name, val) {
                    return {
                        label: name,
                        value: val
                    }
                }));
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                //alert('error - ' + textStatus);
                console.log('error', textStatus, errorThrown);
            }
        });
    },
    minLength: 2,
    select: function (event, ui) {
        alert('you have selected ' + ui.item.label + ' ID: ' + ui.item.value);
        $('#txtSearch3').val(ui.item.label);
        return false;
    }
})
});

//我的控制器代码

 public IDictionary<int, string> Get(string term)
    {
        using (myEntities context = new myEntities())

        {
            return context.Categories1.Where(x => x.CategoryName.Contains(term)).ToDictionary(x => x.CategoryId, x => x.CategoryName);
        }

    }

【问题讨论】:

  • 您能否将您在向 /home/Get 发布 AJAX 请求时收到的 JSON 响应发布?另外你确定响应有这个标题Content-type: application/json; charset=utf-8 吗?
  • 嗨,我的回复是 System.Collections.Generic.Dictionary`2[System.Int32,System.String] 在我添加 Response.ContentType = "application/json";Response.ContentEncoding = System.文本.编码.UTF8;响应永远不会完成.. 我一直看到“加载”或忙碌的 gif。

标签: jquery autocomplete


【解决方案1】:

尝试让您的控制器返回 JSON。

public JsonResult Get(string term)
{
    using (myEntities context = new myEntities())
    {
        return Json(context.Categories1.Where(x => x.CategoryName.Contains(term)).ToDictionary(x => x.CategoryId, x => x.CategoryName));
    }

}

【讨论】:

  • 抱歉没用,我收到一个内部服务器响应,响应头很长,这是一个示例:Type 'System.Collections.Generic.Dictionary`2[[System .Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' 不支持序列化/反序列化对于字典,键必须是字符串或对象。
  • 请尝试ToList(),除非您出于某种原因需要字典。
  • 嗨,我现在得到一个对象数组 -- [{"label":"Careers","value":"2"},{"label":"Construction / DIY", "value":"8"},{"label":"Health","value":"6"},{"label":"信息技术 (IT)","value":"5"},{ "label":"Products","value":"25"},{"label":"Property","value":"1"},{"label":"Travel & Holidays","value": "3"},{"label":"Weddings","value":"7"}] - 但自动完成框显示 [object Object] 条目列表 - 你能告诉我如何将其转换为字符串吗?请? - 谢谢。
【解决方案2】:

好的,终于想通了,我的返回应该只是我的有效 json,不需要标签/值。

感谢您的有用回复

$(document).ready(function () {
$('#txtSearch3').autocomplete({
    source: function (request, response) {
        $.ajax({
            url: '/home/Get',
            type: 'GET',
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: request,
            dataType: 'json',
            success: function (json) {

                response($.map(json, function () {
                    return json;

                }));

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                //alert('error - ' + textStatus);
                console.log('error', textStatus, errorThrown);
            }
        });
    },
    minLength: 2,
    select: function (event, ui) {
        alert('you have selected ' + ui.item.label + ' ID: ' + ui.item.value);
        $('#txtSearch3').val(ui.item.label);
        return false;
    }
})

});

【讨论】:

    猜你喜欢
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 2011-04-02
    • 1970-01-01
    • 2011-02-03
    • 2016-12-27
    • 2017-05-09
    相关资源
    最近更新 更多