Axios快速入门

Axios快速入门

  1. 引入axios的js文件

    <script src="js/axios-0.18.0.js"></script>
    
  2. 使用axios发送请求,并获取响应结果

<!--引入axios的js文件-->
<script src="js/axios-0.18.0.js"></script>

<script>
    // 1. get
    /*axios({
        method:"get",
        url:"http://localhost:8080/ajax-demo/axiosServlet?username=zhangsan"
    }).then(function(resp){
        alert(resp.data)
    });*/

    // 2. post
    axios({
        method:"post",
        url:"http://localhost:8080/ajax-demo/axiosServlet",
        data: "username=zhangsan"
    }).then(function(resp){
        alert(resp.data)
    });
</script>

Axios请求方式别名

<body>

<script src="js/axios-0.18.0.js"></script>

<script>
    // 1. get
    /*axios({
        method:"get",
        url:"http://localhost:8080/ajax-demo/axiosServlet?username=zhangsan"
    }).then(function(resp){
        alert(resp.data)
    });*/

    axios.get("http://localhost:8080/ajax-demo/axiosServlet?username=zhangsan")
    .then(function (resp){
        alert(resp.data)
    });

    // 2. post
    /*axios({
        method:"post",
        url:"http://localhost:8080/ajax-demo/axiosServlet",
        data: "username=zhangsan"
    }).then(function(resp){
        alert(resp.data)
    });*/

    axios.post("http://localhost:8080/ajax-demo/axiosServlet", "username=zhangsan")
        .then(function (resp){
            alert(resp.data)
        });
</script>
</body>