我是靠谱客的博主 默默背包,这篇文章主要介绍json数据的处理,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript"> window.onload = init; function init() { //1、获取部门节点 var dn = document.getElementById("dep"); //2、为该节点创建onchange dn.onchange = getPerson //3、创建一个getPerson的方法来处理事件 } function getPerson() { var did = this.value; //1、获取XMLHttpRequest; var xhr = new XMLHttpRequest(); //2、通过xhr来打开页面,使用POST xhr.open("POST", "personJson.do", true); xhr.onreadystatechange = function() { //3、处理请求 if(xhr.readyState == 4 && xhr.status == 200) { //3.1、获取json var json = xhr.responseText; //如果传递的是json可以直接通过xhr.responseText获取。 //3.2、此时json是一个字符串,如果要转换为对象需要使用eval var ps = eval(json); var node = ""; for(var i = 0; i < ps.length; i++) { //json就是已经是个javascirpt的对象了,可以直接使用 node += ps[i].id + "--------" + ps[i].name + "--------" + ps[i].age + "<br/>"; } //3.4、写入到persons document.getElementById("persons").innerHTML = node; } } xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.send("did=" + did); //4、发送信息,需要传入did }</head> <body> <select id="dep"> <option value="1">普通组</option> <option value="2">明星组</option> <option value="3">西游组</option> </select> <div id="persons"></div> </body> </html>
Json 接受数据是使用 responseText 来接受,默认是字符串,所以要将字符串转换为 json数据,使用 eval 这个全局函数,将字符串转换为 javascript 脚本,javascript 就能识别为 json对象了。

最后

以上就是默默背包最近收集整理的关于json数据的处理的全部内容,更多相关json数据内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(87)

评论列表共有 0 条评论

立即
投稿
返回
顶部