12-Axios的请求***

axios介绍

  • (1)以前 vue-resource
    vue-resource是Vue.js的插件提供了使用XMLHttpRequest或JSONP进行Web请求和处理响应的服务
  • (2)现在(2.0之后) axios
    是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中
    美 [ˈprɑ:mɪs]
    axios的github:https://github.com/axios/axios

axios使用

在这里插入图片描述

  • (1)引入
  • (2)get请求
  • (3)post请求
<body>
		<div id="app">
				<input @click="myget2()" type="button" value="get请求">
				<hr/>
				<input @click="mypost()" type="button" value="post请求">
		</div>
	</body>
	<script>
		//view model
		var vm = new Vue(
				{
					el:'#app', //让vu掌握指定的视频区域
					data:{
					} ,//对应的数据
					methods:{
						myget:function () {
							axios.get('/users/find.do?id=1')
									.then(function (response) {//正常
										console.log(response);
										alert(response.data)
									})
									.catch(function (err) {//请求失败
										console.log(err);
										alert(response.err)
									});

						},
						myget2:function () {
							axios.get('/users/find.do', {
								params: {
									id: 1
								}
							})
							.then(function (response) {
								console.log(response);
							})
							.catch(function (err) {
								console.log(err);
							});


						},
						mypost:function () {

							axios.post('/users/find2.do', {
								id:1
							}) //将{id:1} 这个 json提交给后台,后面需要一个对象来接收 ,参数要加@RequestBody
							.then(function (res) {
								console.log(res);
							})
							.catch(function (err) {
								console.log(err);
							});

						}
					}
				}
		) //创建了MVVM中的VM对象
		
	</script>
  • 将{id:1} 这个 json提交给后台,后面需要一个对象来接收 ,·`参数要加@RequestBody
  • RequestBody将json转换成User对象
@RestController
@RequestMapping("/users")
@Slf4j
public class UserController {
    @RequestMapping(path = "/find.do",method = {RequestMethod.GET})
    public Object get(Integer id){
        log.info("get id "+id);
        User user = new User();
        log.info("get user "+user);
        user.setId(id);
        user.setUsername("jack"+id);
        return user;
    }
    @RequestMapping(path = "/find2.do",method = {RequestMethod.POST})
    public Object post(@RequestBody  User u){//{id:1}
        log.info("post id "+u.getId());
        User user = new User();
        user.setId(u.getId());
        user.setUsername("jack"+u.getId());
        return user;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

翁老师的教学团队

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

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

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

打赏作者

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

抵扣说明:

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

余额充值