axios请求配置:
- url:请求的URL地址
- method:请求的方法,GET可以省略(不区分大小写)
- data:提交数据
形式为:
axios({
url: '请求的地址',
method: '请求方法',
data: {
参数名: 值
}
}).then(result => {
// 对服务器返回的数据进行后续处理
})
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button class="btn">注册用户</button>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
document.querySelector('.btn').addEventListener('click', () => {
axios({
url: 'https://hmajax.itheima.net/api/register',
// 指定请求的方法
method: 'post',
// 提交数据
data: {
username: 'itheima787',
password: '123456'
}
}).then(result => {
console.log(result)
})
})
</script>
</body>
</html>


2413

被折叠的 条评论
为什么被折叠?



