关于springboot与axios整合问题

使用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请求


通过chrome浏览器解析,往后端传递的数据类型为Request Payload。

关于springboot与axios整合问题_第1张图片

如何想接收这段数据,就需要用到@RequestBody注解

 @RequestMapping(value = "/user",method = RequestMethod.POST)
    @ResponseBody
    public void Test(@RequestBody HashMap map){
        System.out.println(map.get("ID"));
    }

这种写法,你就可以通过健值对的形式获取了

你可能感兴趣的:(Springboot,axios)