vue 网络请求demo

异步等待demo:

 

axios-demo:

本地数据显示:

 <!-- 本地数据显示 -->
    <ul v-show="isShowNativeResult">
      <li v-for="(data,index) in datas">
        <span>
          {{data.name}}
        </span>
        <span style="margin-left:10px;">
          {{data.address}}
        </span>
      </li>
    </ul>
    <!-- gankio数据 -->
    <ul v-show="isShowGankIoResult">
      <li v-for="(item,index) in gankIoDatas" class="listItem">
        <img :src="item.url" alt="" style="width:200px">
        <span>整理者:{{item.who}}</span>
      </li>
    </ul>

模拟json 请求:

 // 本地请求 json 模拟
    getData() {
      var that = this
      this.$axios.get('../static/mydata.json')
        .then(response =>{
          console.log(response.data)
          if(response.data.status == '0001'){
            that.datas = response.data.datas
            that.isShowNativeResult = true
            that.isShowGankIoResult = false
          }
        })
        .catch(error =>{
          console.log(error)
          that.isShowNativeResult = false
          that.isShowGankIoResult = false
        })
    },

//这里给出真实的服务器的请求:

 getGanData(){
      var that = this
      // 比如到得干货集中营 福利 10 月 1 号的文章列表
      this.$axios.get('http://gank.io/api/data/福利/10/1')
      .then(response =>{
        console.log(response.data)
        that.gankIoDatas = response.data.results
        that.isShowNativeResult = false
        that.isShowGankIoResult = true
      })
      .catch(error =>{
        console.log('error')
        that.isShowNativeResult = false
        that.isShowGankIoResult = false
      })
    }

考虑3:自定义一个ajax 请求事件:

 <div class="hello">
    <button class="mybtn" type="button" name="button" @click="getData">点击请求</button>
    <ul>
      <li v-for="(data,index) in datas">
        <span>
          {{data.name}}
        </span>
        <span style="margin-left:10px;">
          {{data.address}}
        </span>
      </li>
    </ul>
  </div>

 

//局部和全局使用注册添加一个this:

methods:{
    getData() {
      var that = this
      // 全局注册可以添加一个 this
      this.ajax({
  			methods:'GET',
  			url:'../static/mydata.json',
  			data:{},
  			success:function(res){
  				console.log(res)
          console.log('请求成功,成功的状态码是:'+res.status)
  				if(res.status=='0001'){
            that.datas = res.datas
  				}
  			},
  			faile:function(res) {
          console.log(res)
  			}
  		})
    }
  }
}

考虑4: jquery ajax 点击请求

  <ul>
      <li v-for="(data,index) in datas">
        <span>
          {{data.name}}
        </span>
        <span style="margin-left:10px;">
          {{data.address}}
        </span>
      </li>
    </ul>

脚本:

  

methods:{
    getdata() {
      var that = this
      $.ajax({
        type:'GET',
        url:'../static/mydata.json',
        dataType:"json",
        success:function(res){
          if(res.status =='0001'){
            that.datas = res.datas
          }
          console.log(res)
        },
        error:function(error) {
          console.log('error')
        }
      })
    }
  }

 

 

考虑5 :fetch demo:

<div class="hello">
    <button  class="btn" type="button" name="button" @click="getData">请求本地</button>
    <button  class="btn" type="button" name="button" @click="getGankIO">fetch 请求 gankio 数据</button>
    <!-- 本地请求 -->
    <ul v-show="isShowNativeResult">
      <li v-for="(data,index) in datas">
        <span>
          {{data.name}}
        </span>
        <span style="margin-left:10px;">
          {{data.address}}
        </span>
      </li>
    </ul>

gankio 数据:

 getData() {
      // 本地请求 json
      fetch('../static/mydata.json')
      .then(response =>{
        return response.json()
      })
      .then(myJson=>{
          console.log(myJson)
          if(myJson.status=='0001'){
            this.datas = myJson.datas
            this.isShowNativeResult = true
            this.isShowGankIoResult = false
          }
      })
      .catch(e => {
        console.log('error',e)
        this.isShowNativeResult = false
        this.isShowGankIoResult = false
      })
    },

资源文件:请求文件:
 

<button class="btn" type="button" name="button" @click="getData">点击</button>
     <button class="btn" type="button" name="button" @click="getGanData">请求 gankio 数据</button>
     <!-- 本地数据显示 -->
     <ul v-show="isShowNativeResult">
       <li v-for="(data,index) in datas">
         <span>
           {{data.name}}
         </span>
         <span style="margin-left:10px;">
           {{data.address}}
         </span>
       </li>
     </ul>
     <!-- gankio数据 -->
     <ul v-show="isShowGankIoResult">
       <li v-for="(item,index) in gankIoDatas" class="listItem">
         <img :src="item.url" alt="" style="width:245px">
         <span>整理者:{{item.who}}</span>
       </li>
     </ul>
  </div>

methods:

getData() {
      var that = this
      this.$http.get('../static/mydata.json').then(response =>{
        console.log(response)
        if(response.body.status == '0001'){
          that.datas = response.body.datas
          that.isShowNativeResult = true
          that.isShowGankIoResult = false
        }
      },response => {
        console.log('error')
        that.isShowNativeResult = false
        that.isShowGankIoResult = false
      })
    },

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执于代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值