vue中的请求

vue1的请求

vue-resource

用vue1 的请求,有一个先后顺序需要先引入vue.js,在引入vue-resource.js。
用vue.resource 请求的时候需要在created(){}里面,用this.$http.请求方式

get请求

//http代码:
   <div id='app'>
        <img :src="imgSrc" alt="">
    </div>
//vue实例代码:
      const vm = new Vue({
            el: '#app',
            data: {
                imgSrc: ''
            },
            methods: {
            },
            created() {
                console.log(this);
                this.$http.get('http://wkt.shangyuninfo.cn/weChat/applet/course/banner/list?number=1').then(res => {
                    console.log(res);
                    this.imgSrc = res.data.data[0].imgUrlPc
                }) 
        })

效果如图:
在这里插入图片描述

post请求

post有三种请求头一种是application/JSON;一种是application/formData;另一中是application / x - www - form - urlencoded。
post请求的默认请求头是application/JSON。
如果请求的请求是application / x - www - form - urlencoded这种的话,post请求就有三种参数分别是URL、请求的参数和emulateJSON: true

//引入vue.js
    <script src="./vue-2.4.0.js"></script>
    <script src="./vue-resource-1.3.4.js"></script>
//http代码:
   <div id='app'>
        <img :src="imgSrc" alt="">
    </div>
//vue实例代码:
      const vm = new Vue({
            el: '#app',
            data: {
                imgSrc: ''
            },
            methods: {
            },
            created() {
                console.log(this);
                 //application / x - www - form - urlencoded 请求头
                 this.$http.post('http://wkt.shangyuninfo.cn/weChat/applet/course/list/type', {
                    type: 'free',
                    pageNum: 10,
                    pageSize: 1
                }, { emulateJSON: true }).then(res => {
                    console.log(res);
                })


                //application/JSON请求头             
                this.$http.post('http://wkt.shangyuninfo.cn/weChat/applet/subject/list', {
                    enable: 1
                }).then(res => {
                    console.log(res);
                })
        })

vue2的请求

axios请求

用axios请求前要先引入axios.js
用axios 请求的时候需要在created(){}里面,用axios.请求方式

get请求

//引入vue.js:
    <script src="./vue-2.4.0.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
//css代码:
       * {
            padding: 0;
            margin: 0;
        }

        ul {
            width: 1200px;
            margin: auto;
            list-style: none;
            display: flex;
            justify-content: space-between;
        }

        li {
            width: 18%;
        }

        img {
            width: 100%;
        }
//html代码:
    <div id='app'>
        <img :src="imgSrc" alt="">
    </div>
//vue实例代码:
    const vm = new Vue({
            el: '#app',
            data: {
                imgSrc: '',
                courseList: [],
                boutiqueList: []
            },
            methods: {
            },
            created() {
                console.log(this);
                //  get请求
                axios.get('http://wkt.shangyuninfo.cn/weChat/applet/course/banner/list?number=1').then(res => {
                    console.log(res);
                    this.imgSrc = res.data.data[0].imgUrlPc
                })
        })

效果如下:
在这里插入图片描述

post请求

post有三种请求头一种是application/JSON;一种是application/formData;另一中是application / x - www - form - urlencoded。
post请求的默认请求头是application/JSON。
如果请求的请求头是application / x - www - form - urlencoded或application/formData的话,需要把application / x - www - form - urlencoded或application/formData实例化。

//引入vue.js:
    <script src="./vue-2.4.0.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
//css代码:
      * {
            padding: 0;
            margin: 0;
        }

        ul {
            width: 1200px;
            margin: auto;
            list-style: none;
            display: flex;
            justify-content: space-between;
        }

        li {
            width: 18%;
        }

        img {
            width: 100%;
        }
//html代码:
    <div id='app'>
        <h2>免费课程</h2>
        <ul>
            <li v-for="(item,index) in courseList" :key="item.courseId">
                <img :src="item.coverFileUrl" alt="">
                <p>{{item.courseTitle}}</p>
                <p>{{item.learningNum}}节课 | {{item.participationsCount}}人报名</p>
                <p>免费</p>
            </li>
        </ul>
        <h2>精品课程</h2>
        <ul>
            <li v-for="(item,index) in boutiqueList" :key="item.courseId">
                <img :src="item.coverFileUrl" alt="">
                <p>{{item.courseTitle}}</p>
                <p>{{item.learningNum}}节课 | {{item.participationsCount}}人报名</p>
                <p v-if="item.isFree==1">免费</p>
                <p v-else-if="item.isDiscount==1">{{item.discountPrice}} <del>{{item.coursePrice}}</del></p>
                <p v-else>{{item.discountPrice}}</p>
            </li>
        </ul>
    </div>
//vue实例代码:
    const vm = new Vue({
            el: '#app',
            data: {
                imgSrc: '',
                courseList: [],
                boutiqueList: []
            },
            methods: {
            },
            created() {
                console.log(this);
                  //application / x - www - form - urlencoded请求头
                 let url = new URLSearchParams()
                 url.append('type', 'free')
                 url.append('pageSize', 5)
                 url.append('pageNum', 1)
                 axios.post('http://wkt.shangyuninfo.cn/weChat/applet/course/list/type', url).then(res => {
                     console.log(res);
                     this.courseList = res.data.rows
                  })
                 let urlBoutique = new URLSearchParams()
                 urlBoutique.append('type', 'boutique')
                 urlBoutique.append('pageSize', 5)
                 urlBoutique.append('pageNum', 1)
                 axios.post('http://wkt.shangyuninfo.cn/weChat/applet/course/list/type', urlBoutique).then(res => {
                     console.log(res);
                     this.boutiqueList = res.data.rows
                 })
                 //application/JSON请求头
                 axios.post('http://wkt.shangyuninfo.cn/weChat/applet/subject/list', {
                    enable: 1
                }).then(res => {
                    console.log(res);
                })
        })

效果如下:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值