jQuery的自动完成JSON字符串解析错误 [英] Jquery Autocomplete JSON string parsing error

查看:262
本文介绍了jQuery的自动完成JSON字符串解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 jQuery的自动完成在我的web应用程序,但遇到的问题。我发展我的ASP.NET和JQuery应用。

I want to use Jquery autocomplete in my web application but encounter issues. I am developing my application in ASP.NET and JQuery.

这里的Autocopmlete更迭功能的一部分:

Here's the part of the Autocopmlete 'succes' function:

success: function (data) {
     response($.map(data.d, function (item) {
         return {
              label:  item.key,
             value: item.value
            }
       }));
     },

我的web服务返回以下JSON:

My webservice returns the following JSON:

"[{"key":"Bread","value":"3"}]"

当我运行它,我得到JavaScript错误:

When I run it I get Javascript error:

Uncaught TypeError: Cannot use 'in' operator to search for '42' in [{"key":"bread","value":"3"}] 

它看起来像返回的JSON是不是从什么我可以告诉$ .MAP功能正确的格式。还结果可能返回多个项目,而不仅仅是一个如上面所看到的。

It looks like that the returned JSON is not in the right format for the $.map function from what I can tell. Also the result might return several items, not just one as seen above.

谁能帮我解决这个问题。
我使用 JSON 的dataType GET 作为在Ajax调用键入

Can anyone help me solve this issue. I am using JSON as the dataType and GET as the type in the Ajax call.

推荐答案

我只是建议你,而不是使用您可以用任何其他方法:

I simply suggest you instead of using any other method you can use :

success: function (data, status, xhr) {
    var jsonArray = JSON.parse(data);  // Normal way
}

其他的方式

success: function (data, status, xhr) {
    var jsonArray = $.parseJSON(data); // using jQuery
}

在这种方式将被转换为一个简单的JavaScript对象,你可以很容易地在你的UI操控/ DOM。

In this way it will be converted to a simple JavaScript object which you can easily manipulate on your UI/DOM.

这篇关于jQuery的自动完成JSON字符串解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆