Axios请求方式

Axios请求方式

Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。

使用cdn的方式引入axios.js

<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"/>

使用这个注解可以使前端和后端跨域请求

@CrossOrigin

详细介绍

then成功的回调函数

catch请求失败的异常

params 参数

data请求体数据

axios中文网:https://www.kancloud.cn/yunye/axios/234845

GET请求

方式一:

axios({
  method: 'get',
  url: 'http://127.0.0.1:8081/getAxios',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone'
  }
})
  .then(function(response) {
    console.log(response);
  })
  .catch(function(error) {
    console.log(error);
  });

java后台接收

@ResponseBody
@RequestMapping(value = "/postAxios3", method = RequestMethod.POST)
public Object postAxios3(@RequestBody Map<String, Object> maps) {
	Map<String, Object> map = new HashMap<>();
    map.put("firstName", maps.get("firstName"));
    map.put("lastName", maps.get("lastName"));
   	return map;
}

方式二:

axios.get('http://127.0.0.1:8081/getAxios', {
    params: {
      firstName: 'Fred',
      lastName: 'Flintstone'
    }
  })
  .then(function(response) {
    console.log(response);
  })
  .catch(function(error) {
    console.log(error);
  });

java后台接收

@RequestMapping(value = "/getAxios", method = RequestMethod.GET)
public String getAxios(String firstName, String lastName) {
    return "成功=" + firstName + ":" + lastName;
}

POST请求

方式一:

axios({
  method: 'post',
  url: 'http://127.0.0.1:8081/postAxios',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone'
  }
})
  .then(function(response) {
    console.log(response);
  })
  .catch(function(error) {
    console.log(error);
  });

方式二:

var data = {"firstName":"Fred", "lastName":"lintstone"};
axios.post('http://127.0.0.1:8081/postAxios', data)
  .then(function(response) {
    console.log(response);
  })
  .catch(function(error) {
    console.log(error);
  });

java后台接收

@ResponseBody
@RequestMapping(value = "/postAxios", method = RequestMethod.POST)
public Object postAxios3(@RequestBody Map<String, Object> maps) {
	Map<String, Object> map = new HashMap<>();
    map.put("firstName", maps.get("firstName"));
    map.put("lastName", maps.get("lastName"));
   	return map;
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值