axios初学(1)——get、post请求 发送数据

本文介绍了axios作为vue2.0推荐的HTTP库,如何通过npm或cdn引入,以及如何使用axios进行get、post请求来发送数据给PHP。在get请求中,详细解释了如何传递参数,并提到get.html和post.html中直接引入axios.js可能存在的问题。学习方法包括代码记忆和对比学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vue.js  本身不支持发送ajax,需要 依赖(vue1.0)vue-resource、(vue2.0)axios  jquery(ajax)
vue2.0 不推荐 vue-resource

axios是一个 基于Promise的http请求客户端
用来来发送请求

npm安装
$ npm install axios
bower安装
$ bower install axios
通过cdn
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

get.html:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<script src="../js/vue-2.6.10.js"></script>
	<!-- <script src="../js/axios.js"></script> -->
	<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
	<title>发送get请求</title>
	<script type="text/javascript">
		window.onload=function(){
			new Vue({
				el:'#app',
				data:{
					users:{
						name:'',
						age:''
					}
				},
				methods:{
//axios.get 的发送参数有两种,两个ajax请求函数都可实现
sendGetByStr(){
//1.get通过直接发送字符串拼接
axios.get('get.php?name=${this.users.name}&age=${this.user.age}')
.then(function (response) {
	console.log(response.data);
})
.catch(function (error) {
	console.log(error);
});
},
sendGetByobj(){
	//2.get通过params选项
	axios.get('get.php?',{
		params:{
			name:this.users.name,
			age:this.users.age
		}
	})
		.then(function(response) {
			console.log(response.data);
		})
		.catch(function(error) {
			console.log(error);
		});
}

				}
			});
		}
	</script>
</head>
<body>
	<div id="app">
		<input type="text" :name="users.name" v-model="users.name" placeholder="姓名">
		<input type="text" :age="users.age" v-model="users.age" placeholder="年龄">
		<button @click="sendGetByStr">发送get请求</button>
	</div>
</body>
</html>
get.php:

<?php
	$name=$_GET["name"];
	$age=$_GET["age"];
	echo "姓名:".$name","."年龄:".$age
?>

 

post.html:

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script src="../js/vue-2.4.10.js"></script>
	<script src="../js/axios.js"></script>
	<script>
		window.onload=function(){
			new Vue({
				el:'#app',
				data:{
					users:{
						name:'',
						age:''
					}
				},
				methods:{
					sendPost(){
						axios.post('post.php',{
							name:this.users.name,
							age:this.users.age,
						})
						.then(function (response) {
							console.log(response);
						})
						.catch(function (error) {
							console.log(error);
						});
					}
				}
			});
		}
	</script>
</head>
<body>
		<div id="app">
			<input type="text" :name="user.name" v-model="user.name" placeholder="姓名">
		<input type="text" :age="users.age" v-model="users.age" placeholder="年龄">
		</div>
</body>
</html>
post.php:

<?php
	$name=$_POST["name"];
	$age=$_POST["age"];

	echo "姓名:".$name.","."年龄:".$age
?>

echo "xxx".$num/string.","."xxx".$num/string
xxx 是中文或者 标点符号 逗号
num/string  是被传递的数据,前面一定要有 $ 
比较不理解 这个点 . 是怎么弄的
【待定】


你会发现,通过post  传递的参数 没有在 控制台上,因为 这种方式 传递的数据 是Payload

所以对 post.html 中的methods里的sendPost() 进行改动

sendPost(){
	axios.post('post.php', this.users,{
		//transformRequest 就是用来处理转换的
		transformRequest:[
			function(data)=>{
			let transObj='';
			for(let i in data){
			transObj += i+ '=' + data[i] + '&';
		}
			return transObj;
		}
		]	
})
	.then(function (response) {
	console.log(response.data);
})
	.catch(function (errror) {
	console.log(error);
});
}

发送请求,并且在控制台上 输出: 姓名:xxx,年龄:xxx  背后逻辑代码
通俗的说  axios怎么应用,怎么发送数据给 get.php/post.php (这个应该是 前端了解的了)
也就是 数据传输流程
这里 虽然是 get、post请求,但却是 发送数据给 php,而不是 请求数据,要注意
至于 get.php 和 post.php具体怎么写 好像是后端的事
get.html 发送get请求
get请求有两个参数,也是这两个参数 把name、age名传给 php文件
也就是说  axios.get('xxx')里 xxx 肯定有一句话写着 get.php? 或者 post.php?
这句话 就表明  发过去的数据 和 xxx.php 有关系了

这里有个问题是,get.html 和 post.html 中  我直接  引入的  axios.js  无效 

不知道  是不是 在github上  下载错了

说实话,我看不懂 github  上 那么多 文件夹 ,我只是想找个  可以直接引用 的库啊

对于跨域请求  axios还没有解决方法  
不过可以使用 vue-resource的 jsonp

<!DOCTYPE html>
<html>
<head>
	<script src="../js/vue-2.4.10.js"></script>
	<script src="../js/axios.js"></script>
	<title>输入用户名获取github上的账户信息</title>
	<script type="text/javascript">
		window.onload=function(){
			new Vue({
				el:'#app',
				data:{
					id:'',
					userData:''
				},
				methods:{
					getData(){
						this.$http.jsonp(`https://api.github.com/users/${this.id}`).then(function(resp){
							this.userData=resp.data.data;
						});
					}
				}				
			});
//new Vue({});    后面要加 分号
//getData(){}.then(function(){});  分号;then()函数里面 resp.data.data 不懂
//then  里面是回调函数吗?
		}
	</script>
</head>
<body>
	<div id="app">
		<input type="text" name="" v-model="id" placeholder="姓名">
		<button @click="getData">获取github账户信息</button>
	<div v-for="(v,k) in userData">{{k}}:{{v}}</div>
	</div>
</body>
</html>
//1.点击 “获取github账户信息”的 按钮,调用 vue实例中的 getData方法
//2.getData 方法 写了 怎么 获取github账户信息
this.$http.jsonp() 括号里 写跨域请求 的 地址
.then()函数里 的 参数 就是 返回的数据

显然,$http.jsonp怎么请求到的数据,又怎么返回的  (这句话可能不对)
内部是怎么样的,就不知道了

v-for 列表渲染 拿到的 userData

 

学习方法总结:
1.代码 那些要记住要学会的  就边背写边看  最后完整背写一遍
2.你说我为什么 把代码发到 博客上
因为 是一篇  方便对比  方便随时看  手机上就可以复习
3.源码 放在 笔记本文件夹下 做个存档

原文地址:

https://www.jianshu.com/p/a5fc834ea028

现在 简书 和 思否 都挺不错的,那上面 回答不错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值