了解什么是ajax
-
在不刷新浏览器的发送网络请求 , 并得到服务器响应的数据.
-
(局部刷新)
一. ajax概述
1.1 ajax应用场景
-
微博 的 热搜 (在固定的时间内进行请求 展示最新热搜的数据)
-
百度的搜索框
-
投票 (分享一个 链接 最美大学生 ) 点赞的功能
1.2 GET请求
// 1. 准备发送网络请求
var xhr = new XMLHttpRequest();
// 2. 准备发送网络请求
// xhr.open(请求方式,请求地址);
xhr.open('get', 'http://localhost/getCode.php');
// 3. 发送
xhr.send();
// 4. 监测服务器是否返回了数据
xhr.onload = function () {
// 判断服务器是否返回了数据
// readyState== 4 则代表服务器成功返回了数据
if (xhr.readyState == 4) {
// 服务器已经返回了相关的数据
console.log(xhr.responseText);
}
};
// 下面是jquery 封装好的ajax方法
$.ajax({
url:'请求地址',
data:'参数',
dataType:'json',
success:function(res){
回调函数
}
})
1.2 post请求
post是ajax的另一种请求方式和get有一点不一样
post和get请求 的区别