“Vue 网络应用 axios 基本使用”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“https://www.bilibili.com/video/BV12J411m7MG/?p=25”的新页面) |
Jihongchang(讨论 | 贡献) |
||
(未显示同一用户的7个中间版本) | |||
第1行: | 第1行: | ||
https://www.bilibili.com/video/BV12J411m7MG/?p=25 | https://www.bilibili.com/video/BV12J411m7MG/?p=25 | ||
+ | |||
+ | === axios === | ||
+ | 官网:https://axios-http.com/ | ||
+ | |||
+ | 功能强大的网络请求库 | ||
+ | |||
+ | <syntaxhighlight lang="html"> | ||
+ | <script src="https://unpkg.com/axios/dist/axios.min.js"></script> | ||
+ | axios.get(地址?key1=values&key2=value2).then( | ||
+ | function(response){ | ||
+ | }, | ||
+ | function(err){ | ||
+ | } | ||
+ | ); | ||
+ | |||
+ | axios.post(地址, | ||
+ | { | ||
+ | key1:value1, | ||
+ | key2:value2 | ||
+ | } | ||
+ | ).then( | ||
+ | function(response){ | ||
+ | }, | ||
+ | function(err){ | ||
+ | } | ||
+ | ); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === 随机获取笑话的接口 === | ||
+ | 请求地址:https://autumnfish.cn/api/joke/list | ||
+ | |||
+ | 请求方法:get | ||
+ | |||
+ | 请求参数:num | ||
+ | {| class="wikitable" | ||
+ | |+ | ||
+ | !参数名 | ||
+ | !参数说明 | ||
+ | !备注 | ||
+ | |- | ||
+ | |num | ||
+ | |笑话条数 | ||
+ | |类型为数字 | ||
+ | |} | ||
+ | 响应内容:随机笑话 | ||
+ | |||
+ | === 用户注册接口 === | ||
+ | 请求地址:https://autumnfish.cn/api/user/reg | ||
+ | |||
+ | 请求方法:post | ||
+ | |||
+ | 请求参数:username | ||
+ | {| class="wikitable" | ||
+ | |+ | ||
+ | !参数名 | ||
+ | !参数说明 | ||
+ | !备注 | ||
+ | |- | ||
+ | |username | ||
+ | |用户名 | ||
+ | |不能为空 | ||
+ | |} | ||
+ | 响应内容:注册成功或失败 | ||
+ | |||
+ | === 示例代码 === | ||
+ | <syntaxhighlight lang="html"> | ||
+ | <html> | ||
+ | <head> | ||
+ | <meta charset="UTF-8"> | ||
+ | <meta name="viewport" content="width=device-width,initial-scale=1.0"> | ||
+ | <meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
+ | <title>axios 基本使用</title> | ||
+ | </head> | ||
+ | <body> | ||
+ | |||
+ | <input type="button" value="get 请求" class="get"> | ||
+ | <input type="button" value="post 请求" class="post"> | ||
+ | |||
+ | <!-- 开发环境版本,包含了有帮助的命令行警告 --> | ||
+ | <script src="axios.min.js"></script> | ||
+ | |||
+ | <script> | ||
+ | /* | ||
+ | 接口1:随机笑话 | ||
+ | 请求地址:https://autumnfish.cn/api/joke/list | ||
+ | 请求方法:get | ||
+ | 请求参数:num(笑话条数,数字) | ||
+ | 响应内容:随机笑话 | ||
+ | */ | ||
+ | document.querySelector(".get").onclick= function() { | ||
+ | // axios.get("https://autumnfish.cn/api/joke/list?num=3") | ||
+ | axios.get("https://autumnfish.cn/api/joke/list1234?num=3") | ||
+ | .then(function(response) { | ||
+ | console.log(response); | ||
+ | }, function(err) { | ||
+ | console.log(err); | ||
+ | }) | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | 接口2:用户注册 | ||
+ | 请求地址:https://autumnfish.cn/api/user/reg | ||
+ | 请求方法:post | ||
+ | 请求参数:username(用户名,字符串) | ||
+ | 响应内容:注册成功或失败 | ||
+ | */ | ||
+ | document.querySelector(".post").onclick = function () { | ||
+ | axios.post("https://autumnfish.cn/api/user/reg", { | ||
+ | username:"jack" | ||
+ | }).then(function(response) { | ||
+ | console.log(response); | ||
+ | }) | ||
+ | } | ||
+ | </script> | ||
+ | |||
+ | </body> | ||
+ | </html> | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === 总结、注意 === | ||
+ | |||
+ | * <u>axios</u> 必须先导入才可以使用 | ||
+ | * 使用 <u>get</u> 或 <u>post</u> 方法即可发送对应的请求 | ||
+ | * <u>then</u> 方法中的回调函数会在请求成功或失败时触发:成功走第一个回调函数;失败走第二个回调函数 | ||
+ | * 通过回调函数的形参可以获取响应内容,或错误信息 |
2023年8月9日 (三) 07:44的最新版本
https://www.bilibili.com/video/BV12J411m7MG/?p=25
axios
功能强大的网络请求库
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
axios.get(地址?key1=values&key2=value2).then(
function(response){
},
function(err){
}
);
axios.post(地址,
{
key1:value1,
key2:value2
}
).then(
function(response){
},
function(err){
}
);
随机获取笑话的接口
请求地址:https://autumnfish.cn/api/joke/list
请求方法:get
请求参数:num
参数名 | 参数说明 | 备注 |
---|---|---|
num | 笑话条数 | 类型为数字 |
响应内容:随机笑话
用户注册接口
请求地址:https://autumnfish.cn/api/user/reg
请求方法:post
请求参数:username
参数名 | 参数说明 | 备注 |
---|---|---|
username | 用户名 | 不能为空 |
响应内容:注册成功或失败
示例代码
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>axios 基本使用</title>
</head>
<body>
<input type="button" value="get 请求" class="get">
<input type="button" value="post 请求" class="post">
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="axios.min.js"></script>
<script>
/*
接口1:随机笑话
请求地址:https://autumnfish.cn/api/joke/list
请求方法:get
请求参数:num(笑话条数,数字)
响应内容:随机笑话
*/
document.querySelector(".get").onclick= function() {
// axios.get("https://autumnfish.cn/api/joke/list?num=3")
axios.get("https://autumnfish.cn/api/joke/list1234?num=3")
.then(function(response) {
console.log(response);
}, function(err) {
console.log(err);
})
}
/*
接口2:用户注册
请求地址:https://autumnfish.cn/api/user/reg
请求方法:post
请求参数:username(用户名,字符串)
响应内容:注册成功或失败
*/
document.querySelector(".post").onclick = function () {
axios.post("https://autumnfish.cn/api/user/reg", {
username:"jack"
}).then(function(response) {
console.log(response);
})
}
</script>
</body>
</html>
总结、注意
- axios 必须先导入才可以使用
- 使用 get 或 post 方法即可发送对应的请求
- then 方法中的回调函数会在请求成功或失败时触发:成功走第一个回调函数;失败走第二个回调函数
- 通过回调函数的形参可以获取响应内容,或错误信息