自己封装一个ajax
记得是common.js文件
function objectTostrig(obj) {
let arr = []
for(let k in obj){
arr.push(`${k}=${obj[k]}`)
}
return arr.join('&')
}
function itcast(config) {
let xhr = new XMLHttpRequest()
xhr.onload = function () {
let obj = JSON.parse(xhr.response)
config.success(obj)
}
if (config.method.toUpperCase()=='GET') {
if (typeof config.params=='string') {
xhr.open('GET',config.url+'?'+config.params)
xhr.send()
} else if (typeof config.params=='object') {
xhr.open('GET', config.url + '?' + objectTostrig(config.params))
xhr.send()
} else if (typeof config.params=='undefined') {
xhr.open('GET', config.url)
xhr.send()
}
} else if (config.method.toUpperCase()=='POST') {
if (typeof config.data == "string") {
xhr.open('POST', config.url)
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
xhr.send(config.data)
} else if (typeof config.data == "object") {
xhr.open('POST', config.url)
xhr.setRequestHeader('Content-Type','application/json')
xhr.send(JSON.stringify(config.data))
}
}
}
</script>
</body>
</html>
测试html文件
<!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>
</head>
<body>
\
<script src="./common.js"></script>
<script>
itcast({
method:'get',
url:'http://www.liulongbin.top:3009/api/getbooks',
params:{id:2,bookname:'西游记'},
success:function(res){
console.log(res)
}
})
itcast({
method:'post',
url:'http://www.liulongbin.top:3009/api/addbook',
data:{bookname:"三体四",author:"大飞",publisher:"上海出版社"},
success:function(res){
console.log(res)
}
})
</script>
</body>
</html>
图片上传
<style>
.thumb-box {
text-align: center;
margin-top: 50px;
}
.thumb {
width: 250px;
height: 250px;
object-fit: cover;
border-radius: 50%;
}
</style>
</head>
<body>
<div class="thumb-box">
<img src="./images/cover.jpg" class="img-thumbnail thumb" alt="">
<div class="mt-2">
<input multiple type="file" id="iptFile" accept="image/*" style="display: none;">
<button class="btn btn-primary" id="btnChoose">选择 & 上传图片</button>
</div>
</div>
<script src="./lib/jquery-v3.6.0.js"></script>
<script src="./lib/axios.js"></script>
let inp=document.querySelector('input')
let btn=document.querySelector("button")
let img=document.querySelector('img')
btn.onclick=function(){
// 是通过dom对象直接调用的,不要再前面加on
// 不是所有的事件都能直接调用:mouseover mouseemner
// 能够直接调用的:click() focus() reset()
inp.click()
// 2选择图片后,发送ajax 选择图片触发change事件
inp.onchange=function(){
// 获取文件对象 -input,files是一个文件组成的伪数组
let file= this.files[0];//input的type值等于file的dom对象,上面才有这个属性
// 判断,如果内容为undefinde,给出提示
// console.log(file)
// if(file==undefined){
// return alert('请选择上传文件!')
// }
// 基于文件对象formData对象
let fd= new FormData
//把文件对象,放入formdata对象中,属性以接口文档为准
fd.append('avatar',file)
console.log(...fd)
// 发送axios
axios({
url:'http://www.liulongbin.top:3009/api/upload/avatar',
method:'post',
// 传递参数,不要再fd对象外面再套{}
data:fd
}).then(({data:res})=>{
//判断验证码
// console.log(res)
if(res.code!=200){
return alert(res.message)
}
// 异步代码后执行
// 成功后提示,修改图片路径
alert("恭喜您,上传头像成功");
img.src='http://www.liulongbin.top:3009'+res.url;
})
}
}