获取后台数据使用JQ-AJAX 和 Vue-Axios 两种方式的使用对比

21 篇文章 0 订阅
5 篇文章 0 订阅
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Title</title>
    <script src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

</head>
<body>

    <div id="app">
        <button id="getData">JQ-AJAX获取数据</button>
        <button @click="getAxios">Vue获取数据</button>
    </div>

    <script>
        new Vue({

            el:"#app",
            methods:{

                //使用Vue中的axios获取数据
                getAxios:function(){
                    axios.get('http://jsonplaceholder.typicode.com/users', {
                        params: {
                            ID: 12345
                        }
                    })
                        .then(function (response) {
                            console.log("Vue获取的数据");
                            console.log(response.data);
                        })
                        .catch(function (error) {
                            console.log(error);
                        })
                }
            }
        })

        //使用
        // $("#getData").click(function(){      //ES6之前写法
        $("#getData").click( () =>{             //ES6写法
            $.get("http://jsonplaceholder.typicode.com/users",function(src){
                console.log("JQ-AJAX获取的数据")
                console.log(src)
            })
        })
    </script>
</body>
</html>

在Vue cli 中使用axios

//main.js中的axios配置


//使用Vue - Axios 第 1 步 引入数据请求(当然,要先安装一个axios : npm install --save-dev axios)
import Axios from 'axios'
//使用Vue - Axios 第 2 步 将Axios挂载到原型上
Vue.prototype.$axios = Axios;
//使用Vue - Axios 第 3 步 设置默认请求头
Axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

Vue.config.productionTip = false;   //设置为 false 以阻止 vue 在启动时生成生产提示。

//添加请求拦截器, 当使用post请求时使用
Axios.interceptors.request.use(
    function(config){
      if(config.method == "post"){
        config.data = qs.stringify(config.data)
      }else{
          return config;
      }},
    function(error){
      //返回错误的请求
      return Promise.reject(error);
    }
);
Axios.interceptors.response.use(
    function(response){
      return response;
    },
    //返回错误的请求
    function(error){
      return Promise.reject(error);
    }
)

使用axios

<script>
    export default {
        name: 'layout',
        data () {
            return {
            }
        },
        created(){
            this.$axios.get("http://jsonplaceholder.typicode.com/users",
            ).then(function (response) {
                console.log("Vue获取的数据");
                console.log(response.data);
            }).catch(function (error) {
                console.log(error);
            })
        }
    }
</script>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值