◆axios必须先导入才可以使用
◆使用get或post方法即可发送对应的请求
◆then方法中的回调函数会在请求成功或失败时触发
◆通过回调函数的形参可以获取响应内容,或错误信息
结果:
1,原始:
2,点击get:
3,点击post:
测试错误:
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>axios的基本使用</title>
</head>
<body>
<input type="button" value="get请求" class="get">
<input type="button" value="post请求" class="post">
<!-- 官网提供的axios在线地址 -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
document.querySelector(".get").onclick = function(){
axios.get("https://autumnfish.cn/api/joke/list?num=3")
.then(function (response){
console.log(response);
},function(err){
console.log(err);
})
document.querySelector(".post").onclick = function(){
axios.post("https://autumnfish.cn/api/user/reg",
{username:"jack"})
.then(function(response){
console.log(response);
},function(err){
console.log(err);
})
}
}
</script>
</body>
</html>