axios之post与get请求

axios之post与get请求

axios之post与get请求,如果觉得本片文章对你有帮助期待您的三连,关注公众号获取更多资讯
在这里插入图片描述


前言

提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

一、axios是什么?

axios 是功能强大的网络请求库,基于promise的http库,可以发送get、post请求,也是Vue,React的出现促使axios轻量级库的出现。

二、vue.js+axios的get与post请求

1.get请求

使用cdn:千万不要忘记使用axios前加上以下代码

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

axios的get用法,有点像固定公式,不过最好理解记忆,这里地址和key之间用?间隔,其次分两种情况,成功时回调函数于失败时回调函数

axios.get(地址?key=value&key2=values)
.then(function(response){
成功时回调函数
},function(err){
失败时回调函数
})

实例:

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,minimal-ui:ios">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="">
    <script src=""></script>
</head>

<body>
	<!--axios-->
	<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
	<!--构建一个get请求的按钮-->
	<intput type="button" value="get请求" class="get">
		<script>
		<!--点击get数据-->
		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);
			})
		}
	</script>

</body>

</html>

运行如下:点击get请求按钮,get数据,控制台显示
在这里插入图片描述

2.post请求

axios.post(地址,{key:value,key2:value2})此处与get略有不同


<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

axios.post(地址,{key:value,key2:value2})
.then(function(response){
成功post回调函数
},function(err){
失败回调函数
})

实例:

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,minimal-ui:ios">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="">
    <script src=""></script>
</head>

<body>
	<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
	<intput type="button" value="post请求" class="post">
	<script>
		document.querySelector(".post").onclick = function(){
		axios.axios.post("https://autumnfish.cn/api/user/reg",
            {username:"jack89898kl"})
            .then(function(response){
                console.log(response);
            },function(err){
                console.log(err)
            }
            )
        }}
	</script>
</body>

</html>

运行结果:这里是上传注册用户名jack89898kl
在这里插入图片描述

总结

总而言之,get与post请求的结构是最重要的。
如果觉得本片文章对你有帮助期待您的三连,关注公众号获取更多资讯
在这里插入图片描述

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一坨牛马

你的鼓励是支持我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值