Ajax: 全称(Asynchronous JavaScript And XML),异步的JavaScript和XML。
同步请求发送过程如下图所示:

浏览器页面在发送请求给服务器,在服务器处理请求的过程中,浏览器页面不能做其他的操作。只能等到服务器响应结束后才能,浏览器页面才能继续做其他的操作。
异步请求发送过程如下图所示:

浏览器页面发送请求给服务器,在服务器处理请求的过程中,浏览器页面还可以做其他的操作。
//1. 创建XMLHttpRequest
var xmlHttpRequest = new XMLHttpRequest();
//2. 发送异步请求
xmlHttpRequest.open('GET','http://yapi.smart-xwork.cn/mock/169327/emp/list');
xmlHttpRequest.send();//发送请求
//3. 获取服务响应数据
xmlHttpRequest.onreadystatechange = function(){
//此处判断 4表示浏览器已经完全接受到Ajax请求得到的响应, 200表示这是一个正确的Http请求,没有错误
if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){
document.getElementById('div1').innerHTML = xmlHttpRequest.responseText;
}
}
npm install -g Axios@0.18.0
<script src="js/axios-0.18.0.js"></script>
// 发送 post 请求
axios({
method:"post", // method:请求方式
url:"http://localhost:8080/ajax-demo1/aJAXDemo1?username=zhangsan" // url:链接
data:"username=zhangsan"
}).then(result => {
console.log(result.data);
})
// 发送 get 请求
axios({
method:"get", // method:请求方式
url:"http://localhost:8080/ajax-demo1/aJAXDemo1?username=zhangsan" // url:链接
data:"username=zhangsan"
}).then(result => {
console.log(result.data);
})
| 方法 | 描述 |
|---|---|
| axios.get(url [, config]) | 发送get请求 |
| axios.delete(url [, config]) | 发送delete请求 |
| axios.post(url [, data[, config]]) | 发送post请求 |
| axios.put(url [, data[, config]]) | 发送put请求 |
// 发送 post 请求
axios.post("URL","DATA").then(result => {
console.log(result.data);
})
// 发送 get 请求
axios.get("URL").then(result => {
console.log(result.data);
})