异步的XML和JavaScript,用于浏览器和服务器进行数据交互。
发起一个异步请求
一、GET
(1)get 无参
1.创建一个请求实例
var xhr = new XMLHttpRequest();
2.打开一个链接
xhr.open('get','http://121.0.0.1:5500/findAll');
3.发送请求
xhr.send()
4.监听状态改变 接收响应
xhr.onreadyStateChange = function(){
readyState 0 open未调用 1 open已调用 2接收响应头信息 3.接收响应体信息
if(xhr.readyState ===4 && xhr.status===200){
xhr.response ----json字符串 JSON.parse();
var res = JSON.parse(xhr.response);
res.data.forEach(function(item){
var div = document.createElement('div');
div.innerHTML = item.content;
document.body.appendChild(div)
})
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload = function(){
// 发送一个ajax请求
/**
* 1.创建一个请求实例
*/
var xhr = new XMLHttpRequest();
// console.log(xhr);
/**
* 2.打开一个链接
* open 参数:请求方式 请求路径
*/
xhr.open('get','http://121.199.0.35:8888/index/carousel/findAll');
/**
* 3.发起请求
* send() get请求携带参数携带在地址栏上 post请求携带参数携带在请求体中
*/
xhr.send();
/**
* 接收响应 监听状态改变onreadystatechange
* 响应数据 readyState 4 表示响应完成 status 200表示响应成功
* readyState 0 open未调用 1 open已经调用 2接收响应头信息 3接收响应体信息
* 4.响应完成
*
*/
xhr.onreadystatechange = function(){
// 表示响应完成 表示响应成功
if(xhr.readyState === 4 && xhr.status === 200){
// console.log(xhr.response,'接收响应体');
// 后端返回的数据格式 是json字符串
var res = JSON.parse(xhr.response);
console.log(res);
// 循环创建图片和div 将url给到img src属性
res.data.forEach(function(item){
// console.log(item,'------');
var img = document.createElement('img');
var div = document.createElement('div');
div.innerHTML = item.status + '' + item.introduce;
img.src = item.url;
img.style.width = '200px';
document.body.appendChild(div);
document.body.appendChild(img);
})
}
}
}
</script>
</head>
<body>
</body>
</html>
(2)get 参数
携带在url地址栏 ?name=terry&age=12 查询字符串
1.创建一个请求实例
var xhr = new XMLHttpRequest();
var params = {
page:1,
pageSize:10
}
2.打开一个链接
xhr.open('get','http://121.0.0.1:5500/findAll'+"?"+Qs.stringify(params));
3.发送请求
xhr.send()
4.监听状态改变 接收响应
xhr.onreadyStateChange = function(){
readyState 0 open未调用 1 open已调用 2接收响应头信息 3.接收响应体信息
if(xhr.readyState ===4 && xhr.status===200){
xhr.response ----json字符串 JSON.parse();
var res = JSON.parse(xhr.response);
res.data.forEach(function(item){
var div = document.createElement('div');
div.innerHTML = item.content;
document.body.appendChild(div)
})
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.bootcdn.net/ajax/libs/qs/6.11.1/qs.js"></script>
<script>
window.onload = function(){
/**
* get请求带参数 分页查询 一页查询10条数 page1 pageSize 10
*/
// 1.创建一个xmlHttpRequest请求实例
var xhr = new XMLHttpRequest();
var params = {
page:1,
pageSize:10
}
// 将对象转为查询字符串 Qs.stringify()
var paramsString = Qs.stringify(params);
// ?page=1&pageSize=11 查询字符串
// 2.打开一个链接
xhr.open('get','http://121.199.0.35:8888/index/article/pageQuery'+'?'+paramsString);
// 3.发送请求
xhr.send();
// 4.监听状态改变 接收响应
xhr.onreadystatechange = function(){
if(xhr.readyState ===4 &&xhr.status===200){
var response = JSON.parse(xhr.response);
console.log(response);
var dl = document.createElement('dl');
response.data.list.forEach(function(item){
var dt = document.createElement('dt');
var dd = document.createElement('dd');
//innerText会保留原格式
dt.innerHTML = item.title;
dd.innerHTML = item.content;
dl.appendChild(dt);
dl.appendChild(dd);
document.body.appendChild(dl);
})
}
}
}
</script>
</head>
<body>
</body>
</html>
二、Post
(1)post json
1.创建一个请求实例
var xhr = new XMLHttpRequest();
2.1打开一个链接
xhr.open('post','http://121.0.0.1:5500/user/login');
var params = {
username:'admin1',
password:123321
}
2.2 设置请求头格式
xhr.setRequestHeader('Content-type','application/json;charset=UTF-8');
3.发送请求
xhr.send(JSON.stringify(params))
4.监听状态改变 接收响应
xhr.onreadyStateChange = function(){
readyState 0 open未调用 1 open已调用 2接收响应头信息 3.接收响应体信息
if(xhr.readyState ===4 && xhr.status===200){
xhr.response ----json字符串 JSON.parse();
var res = JSON.parse(xhr.response);
res.data.forEach(function(item){
var div = document.createElement('div');
div.innerHTML = item.content;
document.body.appendChild(div)
})
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
// 1.创建一个请求实例
var xhr = new XMLHttpRequest();
// 2.1打开一个链接
var obj = {
username:'admin1',
password:123321
}
xhr.open('post','http://121.199.0.35:8888/user/login');
// 2.2 设置请求头为json格式 setRequestHeader
/**
* 1.Content-Type application/json 设置json格式
* 2.Content-Type application/x-www-form-urlencoded 设置为表单格式
* 3.Content-Type text/plain;charset=UTF-8 默认值
*/
xhr.setRequestHeader('Content-type','application/json;charset=UTF-8');
// 3.发送请求
xhr.send(JSON.stringify(obj));
// 4.监听状态改变 接收响应
xhr.onreadystatechange = function(){
if(xhr.readyState === 4 && xhr.status === 200){
console.log(JSON.parse(xhr.response));
}
}
</script>
</head>
<body>
</body>
</html>
(2) post 表单格式
1.创建一个请求实例
var xhr = new XMLHttpRequest();
2.1打开一个链接
xhr.open('post','http://121.0.0.1:5500/user/saveOrUpdate');
var params = {
username:'admin1',
password:123321,
telephone:15536332889
}
接口需要表单格式 post的表单格式就是get查询字符串
2.2 设置请求头格式
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded;charset=UTF-8');
3.发送请求
xhr.send(Qs.strinfify(params))
4.监听状态改变 接收响应
xhr.onreadyStateChange = function(){
readyState 0 open未调用 1 open已调用 2接收响应头信息 3.接收响应体信息
if(xhr.readyState ===4 && xhr.status===200){
xhr.response ----json字符串 JSON.parse();
var res = JSON.parse(xhr.response);
res.data.forEach(function(item){
var div = document.createElement('div');
div.innerHTML = item.content;
document.body.appendChild(div)
})
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.bootcdn.net/ajax/libs/qs/6.11.1/qs.js"></script>
<script>
// 1.创建一个请求实例
var xhr = new XMLHttpRequest();
// 2.1打开一个链接
xhr.open('post','http://121.199.0.35:8888/baseUser/saveOrUpdate');
var obj = {
username:'李白',
password:'888888',
telephone:15536332886
};
// 2.2设置请求头格式
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=UTF-8');
// 3.发送请求
xhr.send(Qs.stringify(obj));
// 4.监听状态改变 接收响应
xhr.onreadystatechange = function(){
if(xhr.readyState === 4 && xhr.status===200){
console.log(JSON.parse(xhr.response))
}
}
</script>
</head>
<body>
</body>
</html>