概念:AJAX (Asynchronous JavaScript And XML):异步的JavaScript和XML
AJAX作用:
同步和异步
var xmlhttp;
if (window.XMLHttpRequest){
//code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}else {
//code for lE6,IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET,"url");
xmlhttp.send();//发送请求
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
alert(xmlhttp.responseText);
}
}
Axios 对原生的AJAX进行封装,简化书写
<script src="jslaxios-0.18.0.js"></script>
axios({
method:"get",
url:"http:llocalhost:8080lajax-demo1/aJAXDemo1?username=zhangsan"
}).then(function (resp){
alert(resp.data);
})
axios({
method:"post",
url:"http:/l/localhost:8080/ajax-demo1/aJAXDemo1",
data:"username=zhangsan"
}).then(function (resp){
alert(resp.data);
});
axios.get(“url")
.then(function (resp){
alert(resp.data);
});
axios.post("url","参数")
.then(function (resp){
alert(resp.data);
});
var 变量名 = {"key1": value1,
"key2": value2,
...
};
示例:
var json = {"name: "zhangsan",
"age": 23,
"addr":["北京","上海"","西安"]
};
变量名.key
json.name
Fastjson是阿里巴巴提供的一个Java语言编写的高性能功能完善的JSON库,是目前Java语言中最快的JSON库,可以实现Java对象和JSON字符串的相互转换。
<dependency>
<groupld>com.alibabagroupld>
<artifactld>fastjsonartifactld>
<version>1.2.62version>
dependency>
String jsonStr = JSON.toJSONString(obj);
User user = JSON.parseObject(jsonStr, User.class);