Ajax axios——前后端数据交互

1. Ajax

命令行安装jQuery: npm install jquery

1)$.post():发送post请求

  • 语法:$.post(url, [data], [callback], [type])
  • 参数:
    * url:请求路径
    * data:请求参数
    * callback:回调函数
    * type:响应结果的类型
$.post("ajaxServlet",{username:"rose"},function (data) {
	alert(data);
},"text");

2)$.get():发送get请求

  • 语法:$.get(url, [data], [callback], [type])
  • 参数:
    * url:请求路径
    * data:请求参数
    * callback:回调函数
    * type:响应结果的类型
$.get("ajaxServlet",{username:"rose"},function (data) {
	alert(data);
},"text");

3)ajax vue 参数

var app = new Vue({
	el: "#app",
	data: {
		searchText: '12',
		dis: ''
	},
	methods: {
		btn() {
			var self = this;
			$.ajax({
				// 1. 直接请求不传参数
				url: 'http://localhost:8081/video/listl',
				type: 'GET',
				success: function (response) {
					console.log(response);
					// 将响应数据保存到 Vue 组件的 responseData 中
					self.dis = response;
				},
				
				// 2. 发送请求并在url中传递参数
				url: 'http://localhost:8081/user/list?uname=小红',
				type: 'GET',
				success: function (response) {
					console.log(response);
					// 将响应数据保存到 Vue 组件的 responseData 中
					self.dis = response;
				},
				
				// 3. 发送请求并在data中传递多个参数
				url: 'http://localhost:8081/user/unUpdate',
				type: 'GET',
				data: { 
					uname: '小红',
					uid: 2
				},
				success: function (response) {
					console.log(response);
					// 将响应数据保存到 Vue 组件的 responseData 中
					self.dis = response;
				},
			});
		}
	}
})

2. axios

命令行安装 axios: npm install axios

1)axios 发get请求

//1. 直接请求不传参数
axios.get("http://localhost:9527/springboot-demo/user/list")
.then(res => {
	//then:成功时的回调函数
	//res:后端的响应
	this.userList = res.data;
})

let id = 6;
let name = "张三";
//2. 使用axios发get请求并传递参数
axios.get("http://localhost:9527/springboot-demo/user/detail?id="+id)
.then(res=>{
	console.log("axios传递一个参数>>>>>>",res);
})

//3. 使用axios发get请求传递多个参数
axios.get("http://localhost:9527/springboot-demo/user/select",{
params:{
	userId:id,
	userName:name
}
}).then(res=>{
	console.log("axios传递多个参数>>>>>>",res);
})

//4. 使用路径传参
axios.get("http://localhost:9527/springboot-demo/user/getUserById/"+id)
.then(res=>{
	console.log("axios使用路径传参>>>>>>",res);
})

2)axios 发post请求

new Vue({
	el:"#app",
	data:{
		user:{
			userName:"",
			userAge:""
		}
	},
	created(){ },
	methods:{
		submitData(){
			axios.post("http://localhost:9527/springbootdemo/user/add",this.user)
			.then(res=>{
				console.log(res);
			})
		}
	}
})

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码字小萌新♡

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

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

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

打赏作者

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

抵扣说明:

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

余额充值