目录

一、Get

前端

this.$axios.get('/test', {
	params: {
		id: 1,
		name: 'aa'
	}
})

或者直接把参数拼接在地址后面

this.$axios.get('/test?id=1&name=aa')

后端

@ResponseBody
@GetMapping(value = "/test")
public void test(String id, String name) {
    System.out.println("id:" + id);
    System.out.println("name:" + name);
}

还有一种情况是后端用@PathVariable注解

@ResponseBody
@GetMapping(value = "/test/{id}")
public void test(@PathVariable String id) {
    System.out.println("id:" + id);
}

前端

this.$axios.get("/api/index/test/1")

二、Post

前端

this.$axios.post('/test', {
	data: {
		id: 1,
		name: 'aa'
	}
})

后端一定要使用@RequestBody注解,不然接收不到参数

@ResponseBody
@PostMapping(value = "/test")
public void test(@RequestBody Map<String, Object> map) {
    System.out.println(map.toString());
}

三、Put

put和post一样

this.$axios.put('/test', {
	data: {
		id: 1,
		name: 'aa'
	}
})
@ResponseBody
@PutMapping(value = "/test")
public void test(@RequestBody Map<String, Object> map) {
    System.out.println(map.toString());
}

四、Delete

delete和get一样

this.$axios.delete('/test', {
	params: {
		id: 1
	}
})
this.$axios.delete('/test?id=1')

后端

@ResponseBody
@DeleteMapping(value = "/test")
public void test(String id) {
    System.out.println("id:" + id);
}

也可以使用@PathVariable注解

this.$axios.delete("/api/index/test/1")
@ResponseBody
@DeleteMapping(value = "/test/{id}")
public void test(@PathVariable String id) {
    System.out.println(id);
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐