一、原生AJAX
1、GET请求
GET请求的参数,使用?直接拼接在url地址后面,如果有多个参数使用&符号。
参数例如:name=${name}&pageIndex=${pageIndex}
// 01.创建xhr对象
let xhr = new XMLHttpRequest()
// 02.初始化请求
xhr.open('GET',`http://localhost:5566/students?name=${name}`)
// 03.发送请求
xhr.send()
// 04.监听事件,并接收结果
xhr.onreadystatechange = function(){
//请求完成
if(xhr.readyState===4){
//请求成功
if(xhr.status===200){
console.log(xhr.response); //打印响应结果
}
}
}
2、POST请求
(1)设置Content-Type请求头
POST请求时,需要设置Content-Type请求头,告诉服务器传递的数据格式。
如果是urlencoded格式的数据:
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
如果是json字符串格式的数据:
xhr.setRequestHeader('Content-Type','application/json')
(2)参数传递
post请求的参数,在发送时传递。
传递urlencoded格式的数据:
xhr.send(`name=polo&age=35`)
传递json字符串格式的数据:
let params = {
name:name,
hobbies:hobbies.split(',') //爱好,转为数组
}
xhr.send(JSON.stringify(params)) // 将对象转为json格式字符串
(3)基本格式
let xhr = new XMLHttpRequest()
xhr.open('POST','http://localhost:5566/deleteStudent')
xhr.setRequestHeader('Content-Type','application/json')
xhr.send(JSON.stringify({_id:id}))
xhr.onreadystatechange = function(){
if(xhr.readyState===4){
if(xhr.status===200){
console.log(xhr.response);
}
}
}
二、jQuery AJAX
1、GET请求
语法:$.get(请求地址, {参数名: 参数值}, 回调函数)
$.get('http://localhost:5566/students',{name:"张三"},r=>{
console.log(r); // r表示请求成功时返回的结果数据
})
2、POST请求
语法:$.post(请求地址, {参数名: 参数值}, 回调函数)
$.post('http://localhost:5566/deleteStudent',{_id:id},r=>{
console.log(r); // r表示请求成功时返回的结果数据
})
3、通用型方法ajax
(1)get请求
$.ajax({
type: "get", // 请求的接口地址
url: url, // 请求方式get 或 post
dataType: "json", // 返回的数据类型
// 请求成功后的回调函数
success: function (r) {
console.log(r)
},
// 请求失败后调用的函数
error: function (err) {
console.log('请求错误')
}
});
(2)post请求
发送post请求时,如果请求参数是json字符串格式,需要设置contentType请求头为'application/json'。contentType默认值 "application/x-www-form-urlencoded"。
let params = {
name:name,
hobbies:hobbies.split(',')
}
$.ajax({
url:url, // 请求的接口地址
type:'POST', // 请求方式get 或 post
data:JSON.stringify(params), // 请求的参数
contentType:'application/json',
success:function(r){
console.log(r); // r表示请求成功时返回的结果数据
}
})
三、axios AJAX
1、引入axios库
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.js"></script>
2、GET请求
(1)get请求的参数可以直接用?拼接在URL中
axios.get('http://localhost:5566/user?ID=12345').then(function (response) {
console.log(response);
}).catch(function (error) {
console.log(error);
});
(2)get请求的参数可以写在params对象中
注意:请求返回结果包含很多信息,需要将结果中的data解构出来。data中存放的才是需要的数据。
axios.get('http://localhost:5566/students', {
params: {
name:name
}
}).then(({ data }) => {
console.log(data)
})
3、POST请求
post请求参数直接写在对象中传入。
axios.post(`http://localhost:5566/deleteStudent`, {
name:name,
hobbies:hobbies.split(','),
}).then(({ data }) => {
console.log(data)
})
4、通用方式axios
axios({
method: 'POST', //请求方法
url: '/axios-server', //url
params: { //请求参数
vip: 10,
level: 30
},
headers: { //设置请求头信息
a: 100,
b: 200
},
//请求体参数
data: {
username: 'admin',
password: 'admin'
}
}).then(response => {
console.log(response);
//响应状态码
console.log(response.status);
//响应状态字符串
console.log(response.statusText);
//响应头信息
console.log(response.headers);
//响应体
console.log(response.data);
})
四、fetch AJAX
1、fetch定义
fetch是一个浏览器内置的全新的请求API。之前我们使用的jquery和axios的请求方法只是对XMLHttpRequest对象的封装。
fetch()函数的第一个参数是url地址,第二个参数是配置对象。
2、GET请求
GET请求的参数,使用?直接拼接在url地址后面,如果有多个参数使用&符号。
fetch(`http://localhost:5566/students?stuName=${stuName}`,{
method:'GET', //设置请求方式(默认是GET)
}).then(response=>{
// 第一个then,用于返回请求的状态信息(检查请求是否成功等等)
// 再通过请求状态对象的.json()方法,返回请求结果
return response.json()
}).then(r =>{
console.log(r) // 返回请求结果
})
3、POST请求
发送post请求时,请求参数如果是json字符串格式,需要配置请求头headers,设置Content-Type为'application/json'。
let params = {
name:name,
hobbies:hobbies.split(','),
}
fetch(url,{
method:'POST',
//配置请求头信息
headers:{
'Content-Type':'application/json'
},
body:JSON.stringify(params) // post请求参数
}).then(r=>{
return r.json()
}).then(r=>{
console.log(r) // 返回请求结果
})