Vue框架级的数据请求

  1. 1.axios ( 第三方库 --- 别人封装好的库 )

  2. 2.fetch ( javascript 原生提供 )

  • vue-resource ( Vue 以前自己封装使用的请求类库 ) ,但是 vue-resource作者已经放弃更新了

  • vue-resource 作者推荐我们使用 axios

  • vue-resource 用法 和 axios 相似度 90% +

  • vue2.0我们基本上使用的都是 fetch / axios

  • vue-resource 是有jsonp的

  • vue-resource 如果在vue中使用,是挂载当前的 实例( 组件 ) 的$http属性身上的

    举例 this.$http( options ) this.$http.get() this.$http.postaxios 和 fetch 没有jsonp 数据请求类型的 
  • axios 和 fetch 都是promise

  • axios会对我们请求来的结果进行再一次的封装( 让安全性提高 )

  • 案例:

    axios 中 get请求
new Vue({
      el: '#app',
      methods: {
        getData () {
          //进行get请求
          // axios.get()    -- $.get()
          // axios.post()    ---$.post()
          // axios(options)      -- $.ajax(options)
​
          // var p = axios({
          //   url: './data/data.json'
          // })
​
          // console.log( p )  Promise对象
​
          axios({
            url: './data/data.json',
            method: 'get',//默认就是get请求
          })
            .then( res => console.log( res ))
            .catch( error => conosle.log( error ))
  
​
        },
        postData () {
          // 进行post请求
        },
        get_myself_php_data () {
          axios({
            url: 'http://localhost/get.php',
            params: {
              a: 1,
              b: 2
            }
          })
            .then( res => console.log( res ))
            .catch( error => console.log( error ))
        },
        get_be_data () {
          // 跨域请求线上数据 - 卖座
          axios({
            url: 'https://m.maizuo.com/gateway',
            headers: {
              'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.0.4","e":"154549400038873748996477"}',
              'X-Host': 'mall.film-ticket.film.list'
            },
            params: {
              cityId: 330100,
              pageNum: 1,
              pageSize: 10,
              type: 1,
              k: 7675918
            }
          })  
            .then( res => console.log( res ))
            .catch( error => console.log( error ))
​
        }
      }
    })
  • axios 中 post请求

    
  var params = new URLSearchParams() //得到params对象,用来接收参数
      // params.append( key, value )  key就是参数名,value就是参数值
      params.append( 'a', 2 )
      params.append( 'b', 2 )
      axios({
        url: 'http://localhost/post.php',
        method: 'post',
        headers: {
          'Content-Type': "application/x-www-form-urlencoded" //请求头设置为表单提交的请求头
        },
        data: params
      })
        .then( res => console.log( res ))
        .catch( error => console.log( error ))
  • fetch要手动进行一次数据格式化,但是axios是内部进行了数据的格式化

  • fetch get 方法请求数据,参数要直接连接在url上

  • getData () {
    ​
        fetch('./data/data.json')
    ​
           .then( res =>res.json() ) //对数据进行格式化*
    ​
           .then( data => console.log( data ) ) *// 这里的data就是格式化后的数据*
    ​
           .catch( error => console.log( error ))
         *// fetch('https://m.maizuo.com/gateway?cityId=330100&pageNum=1&pageSize=10&type=1&k=7675918')*
    ​
         }

     

  • fetch 格式化数据 有三种 处理方法

    • .json() 格式化 json 类型数据, 将 json类型 string 转换成 json 对象

    • .text() 格式化文本

    • .blob() 格式化二进制数据

  • fetch 如果按照官网文档书写post请求,也有坑, 携带数据出现了问题

  • fetch post处理

  • postData () {
    
        fetch( 'http://localhost/post.php',{
    ​
          method: 'post',
    ​
          headers: new Headers({
    ​
            'Content-Type': 'application/x-www-form-urlencoded' *// 指定提交方式为表单提交*
    ​
          }),
    ​
          body: new URLSearchParams([["a", 1],["b", 2]]).toString() 
    ​
        })
    ​
          .then( res => res.text() )
    ​
          .then( data => console.log( data ))
    ​
          .catch( error => console.log( error ))
    ​
      }
    • 设置请求头

    • 通过 new URLSearchPrams 来携带参数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值