This time I will bring you jQuery+ajax how to implement json data traversal, what are the precautions for jQuery+ajax to implement json data traversal, the following is a practical case, one Get up and take a look.
Let me tell you my needs first: after making an ajax request, the following json data is passed back in the background.
The specific implementation code is as follows:
JavaScriptCode
{ "data":[ {"id":"1","name":"选择A","value":"A"}, {"id":"2","name":"选择B","value":"B"}, {"id":"3","name":"选择C","value":"C"} ] }
The above json data class is parsed in the success function of jquery
JavaScript code
$.ajax({ type: "POST", url: "xxx.do", dataType : "json", // 指定返回类型 data: {xxx:"yyy"}, // 传递到后台的参数 success: function(data) { $.each(data, function(index,values){ // 解析出data对应的Object数组 $.each(values,function(index2,value){ // 遍历Object数组 ,每个对象的值存放在value ,index2表示为第几个对象 // 根据自己的逻辑进行数据的处理 alert(value.name + " " + value.value); // TODO: 逻辑 }); }); }, error : function() { alert("系统出现问题"); } });
Continue with an example
This chapter shares a code example, which is jQuery encapsulated ajax request processing for JSON data.
The code is relatively simple and can be skimmed by experienced personnel. Beginners can use it as a reference.
The code is as follows:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <title>脚本之家</title> <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> <script> $(document).ready(function(){ $("#bt").click(function(){ $.ajax({ type: "get", dataType: "json", url: "demo/jQuery/ajax/txt/json.txt", success: function(msg){ var data = msg str=""; $.each(data,function(index, n){ str=str+data[index].webName+","+data[index].url+","+data[index].age+"<br/>"; }); $("#show").html(str); } }); }) }) </script> </head> <body> <p id="show"></p> <input type="button" id="bt" value="查看效果"/> </body> </html>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to use the Rating control in AjaxToolKit
##Ajax implements the function of file upload with progress bar effect
The above is the detailed content of How jQuery+ajax implements traversal of json data. For more information, please follow other related articles on the PHP Chinese website!