美文网首页
axios获取数据

axios获取数据

作者: 没了提心吊胆的稗子 | 来源:发表于2019-07-29 20:26 被阅读0次

    Promise封装ajax

    function ajax({url='', type='get', dataType='json'}) {
        return new Promise((resolve, reject)=>{
            let xhr = new XMLHttpRequest;
            xhr.open(type, url, true);
            xhr.responseType = dataType;
            xhr.onload = function () { // onload===xhr.readState=4&&xhr.status=200
                if(xhr.status === 200){
                    (xhr.response);
                }
            };
            xhr.onerror = function (err) {
              reject(err);
            };
            xhr.send();
        })
    }
    

    axios获取数据 获取到的数据是res.data

    axios.get('carts.json').then( res => { // 成功的回调
      // console.log(res);
      this.products = res.data;
    },  err => { // 失败的回调
      console.log(err);
    });
    

    相关文章

      网友评论

          本文标题:axios获取数据

          本文链接:https://www.haomeiwen.com/subject/tfsbrctx.html