01-xhr配置项
<script>
$.ajax({
type: 'get',
url: 'http://127.0.0.1:3001/getHeroSkin',
xhr: () => {
const xhr = new XMLHttpRequest()
return xhr
},
success: (res) => {
console.log(res)
},
})
</script>
</body>
02-上传进度条案例
<!DOCTYPE html>
<html lang="zh-CN">
<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>
<style>
.progress {
background-color: #ddd;
height: 30px;
width: 400px;
margin-top: 20px;
}
.progress-in {
background-color: #cc3636;
height: 30px;
width: 1%;
transition: all 0.4s linear;
}
</style>
</head>
<body>
<input id="file" type="file" />
<button>上传</button>
<!-- 进度条结构 -->
<div class="progress">
<div class="progress-in"></div>
</div>
<script src="./libs/jquery.min.js"></script>
<script>
$('button').click(() => {
const file = document.querySelector('#file').files[0]
if (!file) return alert('请选择文件')
const fd = new FormData()
fd.append('file_data', file)
$.ajax({
type: 'POST',
url: 'http://127.0.0.1:3001/uploadFile',
data: fd,
contentType: false,
processData: false,
xhr: () => {
const xhr = new XMLHttpRequest()
xhr.upload.onprogress = function (e) {
const percentage = Math.round((e.loaded / e.total) * 100) + '%'
$('.progress-in').css({ width: percentage })
}
return xhr
},
success: (res) => {
if (res.code === 200) {
alert('上传成功')
}
},
})
console.log(file)
})
</script>
</body>
</html>
03-xhr配置项工作原理-了解
<!DOCTYPE html>
<html lang="zh-CN">
<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>
<button>点击添加英雄皮肤</button>
<script>
function ajax(options) {
if (!options.type) options.type = 'GET'
if (!options.url) return alert('url必传参数不能省略')
options.type = options.type.toUpperCase()
const params = new URLSearchParams(options.data).toString()
let xhr
if (typeof options.xhr === 'function') {
xhr = options.xhr()
} else {
xhr = new XMLHttpRequest()
}
if (options.type === 'GET') {
if (!params) {
xhr.open(options.type, options.url)
} else {
xhr.open(options.type, options.url + '?' + params)
}
xhr.send()
} else if (options.type === 'POST') {
xhr.open(options.type, options.url)
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
xhr.send(params)
}
xhr.onload = function () {
const res = JSON.parse(xhr.responseText)
if (typeof options.success === 'function') options.success(res)
}
console.log(options)
}
ajax({
type: 'get',
url: 'http://127.0.0.1:3001/getHeroSkin',
data: { heroName: '刘备' },
xhr: () => {
const xhr = new XMLHttpRequest()
return xhr
},
success: (res) => {
console.log(res)
},
})
const button = document.querySelector('button')
button.onclick = function () {
ajax({
type: 'POST',
url: 'http://127.0.0.1:3001/addHeroSkin',
data: {
cname: '刘备',
skin_name: '刘备皮肤',
},
success: (res) => {
console.log(res)
alert('很棒,添加成功')
},
})
}
</script>
</body>
</html>
04-请求超时属性timeout
<!DOCTYPE html>
<html lang="zh-CN">
<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>
<style>
.progress {
background-color: #ddd;
height: 30px;
width: 400px;
margin-top: 20px;
}
.progress-in {
background-color: #cc3636;
height: 30px;
width: 1%;
transition: all 0.4s linear;
}
</style>
</head>
<body>
<input id="file" type="file" />
<button>上传</button>
<!-- 进度条结构 -->
<div class="progress">
<div class="progress-in"></div>
</div>
<script src="./libs/jquery.min.js"></script>
<script>
$('button').click(() => {
const file = document.querySelector('#file').files[0]
if (!file) return alert('请选择文件')
const fd = new FormData()
fd.append('file_data', file)
$.ajax({
type: 'POST',
url: 'http://127.0.0.1:3001/uploadFile',
data: fd,
contentType: false,
processData: false,
timeout: 500,
success: (res) => {
if (res.code === 200) {
alert('上传成功')
}
}
})
console.log(file)
})
</script>
</body>
</html>
05-请求失败回调函数error
<!DOCTYPE html>
<html lang="zh-CN">
<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>
<style>
.progress {
background-color: #ddd;
height: 30px;
width: 400px;
margin-top: 20px;
}
.progress-in {
background-color: #cc3636;
height: 30px;
width: 1%;
transition: all 0.4s linear;
}
</style>
</head>
<body>
<input id="file" type="file" />
<button>上传</button>
<!-- 进度条结构 -->
<div class="progress">
<div class="progress-in"></div>
</div>
<script src="./libs/jquery.min.js"></script>
<script>
$('button').click(() => {
const file = document.querySelector('#file').files[0]
if (!file) return alert('请选择文件')
const fd = new FormData()
fd.append('file_data', file)
$.ajax({
type: 'POST',
url: 'http://127.0.0.1:3001/uploadFile',
data: fd,
contentType: false,
processData: false,
xhr: () => {
const xhr = new XMLHttpRequest()
xhr.upload.onprogress = function (e) {
const percentage = Math.round((e.loaded / e.total) * 100) + '%'
$('.progress-in').css({ width: percentage })
}
return xhr
},
success: (res) => {
if (res.code === 200) {
alert('上传成功')
}
},
timeout: 500,
error: (xhr, textStatus) => {
if (textStatus === 'timeout') alert('网络不佳,请换个位置试试')
},
})
console.log(file)
})
</script>
</body>
</html>