Java大联盟
致力于最高效的Java学习
关注 上一篇 axios 教程中,我已经为大家详细讲解了 axios 异步请求数据的前 6 种操作方式: Spring Boot+Vue|axios异步请求数据的12种操作(上篇) 今天我们继续讲解其余 的 6 种操作。 7、基于 RESTful POST 请求 + 普通变量传参 基于 RESTful 的 axios 异步 POST 请求的方法为 axios.post(url).then() url :请求的 URL,直接追加参数。 then() :请求成功的回调函数。 Vue 代码如下所示。<template> <div> <button type="button" @click="restPostData()">restPostDatabutton><br/> div>template><script> export default {
methods: {
restPostData(){
const _this = this axios.post('http://localhost:8181/restPostData/1').then(function (resp) {
console.log(resp.data) }) } } }script>
Spring Boot 代码如下所示。
@PostMapping("/restPostData/{id}")public User restPostData(@PathVariable("id") Integer id){
System.out.println(id); return new User(1,"张三");}
分 别启动 Vue 和 Spring Boot,点击 restPostData 按钮,结果如下图所示。
8、基于 REST