<!doctype html>
<html lang='en'>
<head>
<title>JSON</title>
<script src="jquery-1.11.0.min.js"></script>
<script>
window.onload = function(){
var obj = {
name:'sonia',
age:20
};
//json格式
//1)JSON中对象要求给属性添加双引号
//2)末尾没有分号
//3)同一个对象中绝对不允许出现两个同名属性
{
"name":"sonia",
"age":20
}
{
"name":"lily",
"hobby":["sing","dance","game"],
"work":{
"job":"web开发",
"address":"wulan"
},
"member":[{
"name":"ben1",
"hobby":["game","sing"],
"member":[{
"name":"ben1-1",
"hobby":["game","sing"]
},{
"name":"ben1-2",
"hobby":["game","sing"]
}]
},
{
"name":"ben2",
"hobby":["game","sing"],
"member":[{
"name":"ben2-1",
"hobby":["game","sing"]
},{
"name":"ben2-2",
"hobby":["game","sing"]
}]
},
{
"name":"ben3",
"hobby":["game","sing"],
"member":[{
"name":"ben3-1",
"hobby":["game","sing"]
},{
"name":"ben3-2",
"hobby":["game","sing"]
}]
}]
}
//JSON.strinfify 过滤
var data = {
"name":"lily",
"hobby":["sing","dance","game"],
"work":{
"job":"web开发",
"address":"wulan"
},
"member":["ben1","ben2","ben3"]
};
console.log(JSON.stringify(data,['name','hobby']));
//AJAX
//XMLHttpRequest XHR ActiveXObject (ie5 ie6)
//1、创建XHR对象 2、连接服务器 3、发送请求 4、接收响应
//var xhr = new XMLHttpRequest();
var xhr;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else {
xhr = new ActiveXObject("Microsoft,XMLHTTP"); //处理IE5 6兼容性
};
xhr.open('get','get.asp?name=lily&age=20',true); //请求类型 true 异步 false同步 get post put delete
//xhr.send();
xhr.send(JSON.stringify({"name":"sonia"})) //post请求传入string
xhr.onreadystatechange = function(){ //readystate变化会触发
if(xhr.readyState ==4 && xhr.status == 200){
console.log(xhr.responseText) //响应数据
}
}
}
</script>
</head>
<body>
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
</ul>
</body>
</html>