vue------axios中的get请求

74 篇文章 3 订阅

一、安装

使用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后面不要写冒号,否则会结束.

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值