Javaweb基础-axios

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

GET方法

get请求第一种写法

java 复制代码
//后端
@Slf4j
@RestController
@RequestMapping("/demo")
public class DemoController {

    @RequestMapping("/getTest")
    // 被@RequestParam标记的参数必传,并且参数名前后端一致
    public Map<String, Object> getTest(@RequestParam String param) {
        log.info("入参:{}", param);
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("param", param);
        log.info("出参:{}", map);
        return map;
    }
}
//axios
export default {
  name: 'App',
  methods: {
    getTest: function () {
      this.$axios({
        method: "GET",        //方法
        url: "/demo/getTest", //访问路径
        params: {             //参数 把参数拼接到url后面=》/demo/getTest?param=hi
          param: "hi"        // 后端被@RequestParam标记的参数名为param
        }
      }).then(res => {        //返回参数
        console.log(res)
      }).catch(err => {       //异常
        alert(err)
      })
    }
  }
}

get请求第二种写法

java 复制代码
//后端
@Slf4j
@RestController
@RequestMapping("/demo")
public class DemoController {

    @RequestMapping("/getTest")
    // 被@RequestParam标记的参数必传,并且参数名前后端一致
    public Map<String, Object> getTest(@RequestParam String param) {
        log.info("入参:{}", param);
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("param", param);
        log.info("出参:{}", map);
        return map;
    }
}
//axios
export default {
  name: 'App',
  methods: {
    getTest2: function () {
      this.$axios
          .get("/demo/getTest?param=hello")    //方法名("访问路径?参数名=参数值"),
          .then(res => {                       //返回数据
            console.log(res)
          })
          .catch(err => {                      //异常
            alert(err)
          })
    },
  }
}

get请求发送的请求不能使用被@RequestBody标记的对象接收。

POST方法

post请求第一种写法

java 复制代码
    //后端
    @RequestMapping(value = "/postTest", method = RequestMethod.POST)
    //被@RequestParam标记的参数必传,并且参数名前后端一致
    public Map<String, Object> postTest(@RequestParam String param) {
        log.info("入参:{}", param);
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("param", param);
        log.info("出参:{}", map);
        return map;
    }
    //axios
export default {
  name: 'App',
  methods: {
    postTest: function () {
      this.$axios({
        method: "POST",            //方法
        url: "/demo/postTest",     //路径
        params: {                  //params中的参数被拼接到url后面=》/demo/getTest?param=hi
          param: "hi"              //post请求中后端使用@RequestParam接参数,使用params
        }
      }).then(res => {
        console.log(res)
      }).catch(err => {
        alert(err)
      })
    },
  }
}  
// axios等价于
export default {
  name: 'App',
  methods: {
    postTest: function () {
        this.$axios.post("/demo/postTest?param=hi") 
          .then(res => {
            console.log(res)
          }).catch(err => {
        alert(err)
      }) 
    },
  }
} 

get请求发送的请求时能使用被@RequestBody标记的对象接收。

java 复制代码
//后端
    @RequestMapping(value = "/postTest2", method = RequestMethod.POST)
    public Map<String, Object> postTest2(@RequestBody Map param) {
        log.info("入参:{}", param);
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("param", param);
        log.info("出参:{}", map);
        return map;
    }
//axios
export default {
  name: 'App',
  methods: {
    postTest: function () {
      this.$axios({
        method: "POST",            //方法
        url: "/demo/postTest2",     //路径
        data: {                  //data中的参数被放置在请求体当中,使用@RequestBody标记的对象接收
          param: "hi"       
        }
      }).then(res => {
        console.log(res)
      }).catch(err => {
        alert(err)
      })
    },
  }
}  

其他方法

delete(url: string);

javascript 复制代码
export default {
  name: 'App',
  methods: {
  //第一种
    deleteTest: function () {
      this.$axios({
        method: "DELETE",
        url: "/demo/deleteTest",
        params: {
          param: "hi"    //同样使用params
        }
      }).then(res => {
        console.log(res)
      }).catch(err => {
        alert(err)
      })
    },
    //第二种
    deleteTest1: function () {
      this.$axios.delete("/demo/deleteTest?param=hi").then(res => {
        console.log(res)
      }).catch(err => {
        alert(err)
      })
    },
  }
}

head(url: string);

javascript 复制代码
export default {
  name: 'App',
  methods: {
    headTest: function () {
      this.$axios({
        method: "HEAD",
        url: "/demo/headTest",
        params: {
          param: "hi"   //同样使用params
        }
      }).then(res => {
        console.log(res)
      }).catch(err => {
        alert(err)
      })
    },
    headTest1: function () {
      this.$axios.head("/demo/headTest?param=hi").then(res => {
        console.log(res)
      }).catch(err => {
        alert(err)
      })
    },
  }
}

options(url: string);

javascript 复制代码
export default {
  name: 'App',
  methods: {
    optionsTest: function () {
      this.$axios({
        method: "OPTIONS",
        url: "/demo/optionsTest",
        params: {
          param: "hi"   //同样使用params
        }
      }).then(res => {
        console.log(res)
      }).catch(err => {
        alert(err)
      })
    },
    optionsTest1: function () {
      this.$axios.options("/demo/optionsTest?param=hi").then(res => {
        console.log(res)
      }).catch(err => {
        alert(err)
      })
    },
  }
}

put(url: string, data?: any);

javascript 复制代码
export default {
  name: 'App',
  methods: {
    putTest: function () {
      this.$axios({
        method: "PUT",
        url: "/demo/putTest",
        data: {
          param: "hello"    //使用data和params都可以
        }                    //data放置在请求体,params拼接在请求路径
      }).then(res => {
        console.log(res)
      }).catch(err => {
        alert(err)
      })
    },
    putTest1: function () {
      this.$axios.put("/demo/putTest", {param: "hello"}).then(res => {
        console.log(res)
      }).catch(err => {
        alert(err)
      })
    },
  }
}

patch(url: string, data?: any);

javascript 复制代码
export default {
  name: 'App',
  methods: {
    patchTest: function () {
      this.$axios({
        method: "PATCH",
        url: "/demo/patchTest",
        data: {
          param: "hello"     //使用data和params都可以
        }                    //data放置在请求体,params拼接在请求路径
      }).then(res => {
        console.log(res)
      }).catch(err => {
        alert(err)
      })
    },
    patchTest1: function () {
      this.$axios.patch("/demo/patchTest", {param: "hello"},).then(res => {
        console.log(res)
      }).catch(err => {
        alert(err)
      })
    },
  }
}
相关推荐
速易达网络6 小时前
RuoYi、Vue CLI 和 uni-app 结合构建跨端全家桶方案
javascript·vue.js·低代码
lyj1689977 小时前
vue-i18n+vscode+vue 多语言使用
前端·vue.js·vscode
我在北京coding10 小时前
TypeError: Cannot read properties of undefined (reading ‘queryComponents‘)
前端·javascript·vue.js
海天胜景11 小时前
vue3 获取选中的el-table行数据
javascript·vue.js·elementui
翻滚吧键盘11 小时前
vue绑定一个返回对象的计算属性
前端·javascript·vue.js
乆夨(jiuze)12 小时前
记录H5内嵌到flutter App的一个问题,引发后面使用fastClick,引发后面input输入框单击无效问题。。。
前端·javascript·vue.js
小彭努力中12 小时前
141.在 Vue 3 中使用 OpenLayers Link 交互:把地图中心点 / 缩放级别 / 旋转角度实时写进 URL,并同步解析显示
前端·javascript·vue.js·交互
xiguolangzi13 小时前
vue3+element-plus el-table列的显隐、列宽 持久化
前端·javascript·vue.js
大猩猩X13 小时前
vxe-upload vue 实现附件上传、手动批量上传附件的方式
vue.js·vxe-ui
come1123414 小时前
Vue 响应式数据传递:ref、reactive 与 Provide/Inject 完全指南
前端·javascript·vue.js