jquery解析json报错_jQuery的自动完成JSON字符串解析错误

bd96500e110b49cbb3cd949968f18be7.png

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

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

}

}));

},

My webservice returns the following JSON:

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

When I run it I get Javascript error:

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

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.

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

}

Other way

success: function (data, status, xhr) {

var jsonArray = $.parseJSON(data); // using jQuery

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值