ajax get请求得到了一个json格式的数据,在js中如何遍历出来

WBOY
Release: 2016-06-13 12:09:16
Original
1425 people have browsed it

ajax get请求得到了一个json格式的数据,在js中怎么遍历出来
nbsp;html>



    <script></script>
    <script><br /> $(document).ready(function(){<br /> $("button").click(function(){<br /> $.get("/world",function(data,status){<br /> //data为json格式 返回数据位: {"name":"liweitao","age":"23"}<br /> //在js中我怎么得到name的值,怎么遍历<br /><br /> <span style="color: #FF0000;"> //请帮我在此处补充代码<br /><br /> });<br /> });<br /> });<br /> </script>







------解决思路----------------------
<br /><script type="text/javascript"><br />var data = {"name":"liweitao","age":"23"};<br />for(var obj in data){<br />    document.writeln(obj + ':' + data[obj]);<br />}<br /></script><br />
Copy after login

------解决思路----------------------
//data为json格式  返回数据位: {"name":"liweitao","age":"23"}<br />$.get("/world",function(data,status){<br />  //在js中我怎么得到name的值,怎么遍历<br />  var d = eval('(' + data + ')');<br />  alert(d.name);<br />});<br />
Copy after login

$.get("/world",function(data,status){<br />  //在js中我怎么得到name的值,怎么遍历<br />  alert(data.name);<br />}, 'json'); //如果加上这个属性<br />
Copy after login

------解决思路----------------------
for in 循环或者直接for循环,但是如果直接for循环的话你就得使用$.ajax来重新调整你的代码了。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!