• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

23-Ajax-axios

武飞扬头像
破烂儿
帮助1

一、原生Ajax

学新通

学新通

<!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>
学新通

学新通

学新通

二、Axios

学新通

<!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>
学新通

学新通

学新通

三、Axios案例

学新通

<!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>
学新通

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgaebhf
系列文章
更多 icon
同类精品
更多 icon
继续加载