ajax 与 axios区别
第一种 ajax
var str='HELLO , 自由网'
$.ajax({
url:"test.php",
data:{str:str},
type:'post',
success:function(msg){
console.log(msg)
}
})
Ajax 是一种用于创建快速动态网页的技术
是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术
请求 头
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,und;q=0.7
Connection: keep-alive
Content-Length: 41
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
application/x-www-form-urlencoded; charset=UTF-8
我们可以看到 ajax FormData
类型
axios 类型的
支持浏览器和node.js
支持promise
能拦截请求和响应
能转换请求和响应数据
能取消请求
自动转换JSON数据
浏览器端支持防止CSRF(跨站请求伪造)
axios.post(
'test.php',
).then((response=>{
console.log(response)
}))
请求头
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,und;q=0.7
Connection: keep-alive
Content-Length: 0
默认axios提交数据类型是Payload,Content-Type:"application/json"
而后台接不到数据, 此时此刻 我们要将 json 格式转换为 str=1这种格式
var params = new URLSearchParams();
params.append('str', '自由网');
params.append('pass', '123');
console.log(params)
axios.post(
'test.php',
params
).then((response=>{
console.log(response)
}))
发起多个 axios请求
function axios1(){
axios.get('test.php?type=1')
}
function axios2(){
axios.get('test.php?type=2')
}
axios.all([axios1(),axios2()]).then( )
自定义配置发送 config
str = '123'
axios({
method:'post',
url:"test.php",
data:{
str:str
}
})