澳门银河网上官方赌场_老品牌值得信赖

ITKeyword,专注技术干货聚合推荐

注册 | 登录

javascript - Uncaught TypeError: Cannot use 'in' operator to search for '161' in

itPublisher 分享于

2020腾讯云限时秒杀,爆款1核2G云服务器99元/年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2020阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

up vote 0 down vote favorite I have some javascript code that is making an ajax call when I change a dropdownlist. It just passes the selected value from the dropdown and gets a collection related to the selected value: $("#ddl").change(function () {

$.ajax({

type: "POST",

url: "/MyService.asmx/GetCollection",

data: "{'selectedId':'" + $(this).val() + "'}",

contentType: "application/json; charset=utf-8",

dataType: "json",

success: function (response) {

$.each(response.d, function (i, item) {

alert(item.Text);

});

}

});

})

This works correctly, and this is the json response I'm getting from my ajax call. d:

"[{"Id":"c709b6d3-5841-4240-b25c-9f730530a998","Text":"Item 1"},

{"Id":"c7dd6ee0-836a-4b19-9b88-f8f2455b6e32","Text":"Item 2"}]"

But when I try and loop through each item in response.d and alert item.Text, I'm getting this error message: Uncaught TypeError: Cannot use 'in' operator to search for '161' in [{"Id":"c709b6d3-5841-4240-b25c-9f730530a998","Text":"Item 1"},{"Id":"c7dd6ee0-836a-4b19-9b88-f8f2455b6e32","Text":"Item 2"}] Am I looping through this wrong? Or am I outputting the content incorrectly? javascript jquery json
  |
  this question asked Sep 26 '15 at 17:36 Steven 5,879 48 137 224 2   response.d is string, you cannot use each on string, parse it to JSON and then use each on it –  Tushar Sep 26 '15 at 17:37      @Tushar - can I do that in jQuery? –  Steven Sep 26 '15 at 17:39 3   Use var d = JSON.parse(response.d) and then loop on d –  Tushar Sep 26 '15 at 17:40



 |  1 Answers

up vote 1 down vote ---Accepted---Accepted---Accepted---

Like @Tushar mentioned in the comment you have to use a loop, for example for loop like following : var response = {d: [{"Id":"c709b6d3-5841-4240-b25c-9f730530a998","Text":"Item 1"}, {"Id":"c7dd6ee0-836a-4b19-9b88-f8f2455b6e32","Text":"Item 2"}]};

for(var i=0;i<response.d.length;i++){

alert(response.d[i].Text);

}

If the response id string use JSON.parse : var response = JSON.parse('{"d": [{"Id":"c709b6d3-5841-4240-b25c-9f730530a998","Text":"Item 1"}, {"Id":"c7dd6ee0-836a-4b19-9b88-f8f2455b6e32","Text":"Item 2"}]}')

Hope this helps
  |
  this answer answered Sep 26 '15 at 17:50 Zakaria Acharki 34.1k 10 28 57



 | 

up vote 0 down vote favorite I have some javascript code that is making an ajax call when I change a dropdownlist. It just passes the selected value from the dropdown and gets a co

相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。

澳门银河网上官方赌场