json数据在前端遍历注意事项
来源:互联网 发布:linux sys.h 编辑:程序博客网 时间:2024/05/16 05:45
方法一:使用jquery
$.ajax({type : "POST" ,async : true,url :"${pageContext.request.contextPath}/JqueryAjaxDemo",data : {"id" : 123 , "name" : "zxc"}, //传递参数dataType : "json", //规定了返回类型 也可以不规定 直接$.each($.parseJSON(result),function(id,json){});error : function(){alrt('error');},success : function(result){$.each(result,function(id,json){$("#ajax2").append(id+json.id+json.name+json.age);$("#ajax3").html("<h1>hello</h1>");$("#ajax4").text("<h1>hello</h1>");});}});方法二:使用eval()
var xmlhttp;if(window.XMLHttpRequest){xmlhttp = new XMLHttpRequest();}else{xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}xmlhttp.onreadystatechange=function() {if(xmlhttp.readyState==4 && xmlhttp.status==200){//返回值格式 {"age":1,"id":1,"name":"zxc"} 注意 不是{fname:"John",lname:"Doe",age:25}这种格式var json = eval('('+xmlhttp.responseText+')');for(var x in json ){alert(json[x]);} document.getElementById("ajax1").innerHTML =json.id+json.name+json.age;}}xmlhttp.open("get","${pageContext.request.contextPath}/AjaxDemo_json?"+Date(),true);xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");xmlhttp.send(); }注意:{"age":1,"id":1,"name":"zxc"}是json返回数据的格式,key带有引号“” ,需要用eval()函数转成{age:1,id:1,name:"zxc"}格式,才能用for循环在js中遍历
阅读全文
1 0
- json数据在前端遍历注意事项
- json数据在前端和php后端处理注意事项
- 在js中遍历JSON数据
- 使用JQuery 将DataTable 转化成JSON数据,前端无法处理JSON数据的注意事项
- MVC 后台返回Json 并在前端进行遍历demo
- js遍历json数据
- js遍历json数据
- 简单遍历json数据
- jquery遍历JSON数据
- jquery遍历json数据
- JSONCPP遍历JSON数据
- JSONCPP遍历JSON数据
- json 后端list 传到前端并遍历
- 前端js遍历json对象方法
- .net 获取json前端数据
- .net 返回前端json数据
- json在前端的运用
- JS 循环遍历JSON数据
- hdu6139
- 熟能生巧vector
- volatile和synchronized
- MySql笔记(索引)
- DNS服务器可能不可用
- json数据在前端遍历注意事项
- Sql中表的操作
- textrank 关键词提取-python实现
- 欢迎使用CSDN-markdown编辑器
- Linux内核学习总结
- IntelliJ IDEA关于SSH超级简单的项目
- 算法的特征
- 'latin-1' codec can't encode characters问题
- java中的switch语句