vue axios achieve data request

1. Install nginx 

npm install axios --save-dev
cnpm install axios --save-dev

Use a mirror Taobao

Save the file to the local dependence

Fitted. packjson.js package management there will be

2. all dependent packages can be managed directly import code inside

<script>
import axios from 'axios';

3. Request achieve

Created () { 
       Axios ({ 
                Method : 'GET', 
                URL : 'HTTP: // localhost / ****** your request path' 
            }) .then ( function () { 
                Console . log ( 'sssss' ); 
            }); 
  } ,

4. View Data

The background is determined if not done on a single post and get back the data processed goods, the front end can easily write post or get requests.

6. can be achieved, this is my Xia Xie. Method when the actual project request, axios certainly go a unified exposed, not written, otherwise can not be unified management.

 

axios promise is based on the method es6

jquery里面的ajax其实是封装的,利用new XMLHttpRequest(),js的,浏览器内嵌方法。和 new Array()差不多。Http协议请求

var Ajax = {
    get: function(url,fn){
        // XMLHttpRequest对象用于在后台与服务器交换数据
        var xhr=new XMLHttpRequest();
        xhr.open('GET',url,false);
        xhr.onreadystatechange=function(){
            // readyState == 4说明请求已完成
            if(xhr.readyState==4){
                if(xhr.status==200 || xhr.status==304){
                    console.log(xhr.responseText);
                    fn.call(xhr.responseText);
                }
            }
        }
        xhr.send();
    },

    // data应为'a=a1&b=b1'这种字符串格式,在jq里如果data为对象会自动将对象转成这种字符串格式
    post: function(url,data,fn){
        var xhr=new XMLHttpRequest();
        xhr.open('POST',url,false);
        // 添加http头,发送信息至服务器时内容编码类型
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xhr.onreadystatechange=function(){
            if (xhr.readyState==4){
                if (xhr.status==200 || xhr.status==304){
                    // console.log(xhr.responseText);
                    fn.call(xhr.responseText);
                }
            }
        }
        xhr.send(data);
    }
}

 

Guess you like

Origin www.cnblogs.com/chenyi4/p/12335983.html