使用axios的时候一定要引用它的js包(可用npm命令:npm install axios)
1.get请求
// 为给定 ID 的 user 创建请求
axios.get('/demo/name')
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
response用于接收后端的数据,而response.data正好对应后端传入的 hello
后端接收
@RequestMapping(value = "/name",method = RequestMethod.GET)
@ResponseBody
public String Testname(){
return "hello";
}
2.post请求
这是一段前端发送请求代码,利用axios发送post请求
<button type="button" onclick="openUrl()">testaxios</button>
<script>
function openUrl(){
axios({
method: 'post',
url: '/demo/user',
data: {
ID: 'Fred',
lastName: 'Flintstone'
}
});
}
</script>
通过chrome浏览器解析,往后端传递的数据类型为Request Payload。

如何想接收这段数据,就需要用到@RequestBody注解
@RequestMapping(value = "/user",method = RequestMethod.POST)
@ResponseBody
public void Test(@RequestBody HashMap<String,String> map){
System.out.println(map.get("ID"));
}
这种写法,你就可以通过健值对的形式获取了
使用Axios进行HTTP请求

本文详细介绍了如何使用Axios库进行GET和POST请求。GET请求用于从服务器获取数据,而POST请求则用于向服务器发送数据。文章通过具体示例展示了前端如何与后端进行交互,包括数据的接收和发送。
427

被折叠的 条评论
为什么被折叠?



