vue axios中的get请求方式

2022-10-14,,,

这篇文章主要介绍了vue axios中的get请求方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

目录
  • vue axios中的get请求
    • 一、安装
    • 二、使用
    • 常见错误
  • vue axios post请求参数错误400

    vue axios中的get请求

    一、安装

    使用 npm:npm install axios

    二、使用

    步骤:1.引入 2.发送请求

    <template>
      <div>
        <!-- 2.点击发送请求 -->
        <button @click="getdata">get请求-无参数</button
        ><button @click="getDataByParams">get请求-有参数</button>
      </div>
    </template>
    <script>
    //1.引入axios
    import axios from "axios";
    export default {
      methods: {
        // 3.发送axios无参数请求
        getdata() {
          axios
            // 3.1url地址
            .get("http://157.122.54.189:9095/scenics/banners")
            // 3.2成功时回调函数
            .then((data) => {
              console.log(data);
            })
            //3.3失败时回调函数
            .catch((err) => {
              console.log(err);
            });
        },
        // 有参数
        getDataByParams() {
          axios
            //params:可传递多个参数,固定写法,不能改,否则参数传递失败
            .get("http://157.122.54.189:9095/posts", { params: { id: 4 } })
            .then((data) => {
              console.log(data);
            })
            .catch((err) => {
              console.log(err);
            });
        },
      },
    };
    </script>
    <style>
    </style>

    常见错误

    url后面不要写冒号,否则会结束。 

    vue axios post请求参数错误400

    如果直接把loginMode1当请求参数传,后端是接收不到的

    要对loginMode1处理成字符串然后再转换,如下

    然后再axios处理

    这样就不会有问题了

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持北冥有鱼。

    您可能感兴趣的文章:

    • vue项目及axios请求获取数据方式
    • Vue axios库发送请求的示例介绍
    • Vue使用axios发送请求并实现简单封装的示例详解
    • vue结合axios实现restful风格的四种请求方式
    • Vue3如何使用axios发起网络请求
    • Vue使用axios添加请求头方式
    • vue中数据请求axios的封装和使用
    • Vue取消Axios发出的请求

    《vue axios中的get请求方式.doc》

    下载本文的Word格式文档,以方便收藏与打印。