23-Ajax-axios

一、原生Ajax

image-20230414220216576

image-20230414220238724

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="button" value="获取数据" onclick="getData()">
    <div id="div1"></div>
</body>
<script>
    //Ajax 的核心是 XMLHttpRequest 对象。
    // 1、创建XMLHttpRequest对象
    var xmlhttprequest = new XMLHttpRequest();
    // 2、发送异步请求
    xmlhttprequest.open("GET","http://yapi.smart-xwork.cn/mock/169327/emp/list");
    xmlhttprequest.send();
    // 3、获取服务器响应数据
    xmlhttprequest.onreadystatechange = function () {
        if(xmlhttprequest.readyState==4 && xmlhttprequest.status==200){
            document.getElementById("div1").innerHTML = xmlhttprequest.responseText;
        }
      }
</script>
</html>

image-20230414220452638

image-20230414224338655

二、Axios

image-20230414224750589

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/axios-0.18.0.js"></script>
</head>
<body>
    <input type="button" value="get" onclick="getMethod()">
    <input type="button" value="post" onclick="postMethod()">
</body>
<script>
    function getMethod() {
/*         axios({
        method:"get",
        url:"http://yapi.smart-xwork.cn/mock/169327/emp/list"
    }).then((result)=>{//成功回调函数
        console.log(result.data);
    }) */
    // 更加简便的方法:
    axios.get("http://yapi.smart-xwork.cn/mock/169327/emp/list").then((result)=>{
        console.log(result.data);
    })
    }
    function postMethod(){
/*         axios({
            method:"post",
            url:"http://yapi.smart-xwork.cn/mock/169327/emp/deleteById",
            data:"id=1"
        }).then((result)=>{
            console.log(result.data);
        }) */
        axios.post("http://yapi.smart-xwork.cn/mock/169327/emp/deleteById","id=1").then((result)=>{
            console.log(result.data);
        })
    }
</script>
</html>

image-20230414231154562

image-20230414231220171

三、Axios案例

image-20230414231258320

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>axios案例</title>
    <script src="js/vue.js"></script>
    <script src="js/axios-0.18.0.js"></script>
    <style>
        #app{
            max-width: 1680px;
            width:80%;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div id="app">
        <table border="1" cellspacing="0" style="margin: auto;">
            <tr>
                <th>编号</th>
                <th>姓名</th>
                <th>图像</th>
                <th>性别</th>
                <th>职位</th>
                <th>入职日期</th>
                <th>最后操作时间</th>
            </tr>
            <tr v-for="(user,index) in userall">
                <td>{{index+1}}</td>
                <td>{{user.name}}</td>
                <!-- 这里比较容易出错,图片必须用img标签渲染,然后src要用vue-bind:src或者:src -->
                <td><img v-bind:src="user.image" width="100px"></td>
                <td v-if="user.gender==1"></td>
                <td v-if="user.gender==2"></td>
                <td>{{user.job}}</td>
                <td>{{user.entrydate}}</td>
                <td>{{user.updatetime}}</td>
            </tr>

        </table>
    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data:{
            userall:[]
        },
        methods:{

        },
        mounted(){
            axios.get("http://yapi.smart-xwork.cn/mock/169327/emp/list").then((result)=>{
                this.userall=result.data.data;
            });
        }
    })
</script>
</html>

image-20230414235131885

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要安装vue-axios,您可以使用npm或yarn。在命令行中运行以下命令来安装vue-axios-interceptors: ``` npm install vue-axios-interceptors --save ``` 或 ``` yarn add vue-axios-interceptors ``` 请确保在引入vue之后导入此包。在main.js文件中引入axios并将其设置为Vue的原型属性,然后将axios传递给Vue实例中的axios选项: ``` import axios from 'axios' Vue.prototype.axios = axios; new Vue({ axios, router, store, render: h => h(App) }).$mount('#app') ``` 这样,您就可以在整个Vue应用程序中使用axios了。希望对您有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue-axios-interceptors:全局捕获和处理ajax响应](https://download.csdn.net/download/weixin_42097557/14984413)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [vue-axios的安装及四个常见方法](https://blog.csdn.net/Rich_lady/article/details/125082024)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Vue开发实例(13)之axios和mockjs的安装与使用](https://blog.csdn.net/dkm123456/article/details/124444369)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值