Home > Web Front-end > JS Tutorial > body text

How jQuery+ajax implements traversal of json data

php中世界最好的语言
Release: 2018-04-04 11:45:01
Original
1535 people have browsed it

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"} 
] 
}
Copy after login

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("系统出现问题"); 
} 
});
Copy after login

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:

 
 
 
 
脚本之家 



Copy after login

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!

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!