美文网首页
axios API的基本使用方法

axios API的基本使用方法

作者: 小黄不头秃 | 来源:发表于2023-06-08 02:15 被阅读0次

axios 是一个基于Promise用于浏览器和node.js的http客户端,它具有以下特征:
(1)支持浏览器和node.js
(2)支持promise
(3)能拦截请求和响应
(4)自动转换JSON数据

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 
        axios 是一个基于Promise用于浏览器和node.js的http客户端,它具有以下特征:
        (1)支持浏览器和node.js
        (2)支持promise
        (3)能拦截请求和响应
        (4)自动转换JSON数据
     -->
     <script src="./axios/axios.js"></script>
     <script>
         axios.get('https://localhost:3000/data').then(function(ret){
            console.log(ret.data);
         });
     </script>
</body>
</html>

其常用API的使用方法:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 
        get:查询数据
            通过url传递参数
            通过params选项传递参数
        post:添加数据
            通过选项传递参数(默认传递的是json格式)
            通过URLSearchParams传递参数(application/x-www-form-urlencoded)
        put:修改数据
            和post的方法极为类似
        delete:删除数据
            和get的方法是一样的使用params传递参数
     -->
     <script src="./axios/axios.js"></script>
     <script>
         axios.get('http://localhost:3000/axios?id=123').then(function(ret){
             console.log(ret.data)
         });
         axios.get('http://localhost:3000/axios',{
             params:{
                 id:1
             }
         }).then(function(ret){
             console.log(ret.data)
         });
         axios.delete('http://localhost:3000/axios',{
             params:{
                 id:1
             }
         }).then(function(ret){
             console.log(ret.data)
         });
         axios.post('http://localhost:3000/axios',{
             id:2,
             uname:"sunwukong"
         }).then(function(){
            console.log(ret.data);
         });
         var params = new URLSearchParams();
         params.append('uname','lisi');
         params.append('psw',123);
         axios.post('https://localhost:3000/axios',params).then(function(ret){
             console.log(ret.data)
         })
     </script>
     
</body>
</html>

axios 处理响应结果:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 
        响应结果的主要属性:
        data:实际响应回来的数据
        headers:响应头信息
        status:响应状态码
        statusText:响应状态信息

        axios的全局配置
        axios.defaults.timeout=3000;//超时时间
        axios.defaults.baseURL='http://localhost:3000/app';//默认地址
        axios.defaults.headers['mytoken']='aqwerwqwerqwer2ewrwe23eresdf23'//设置请求头
     -->
     <script src="./axios/axios.js"></script>
     <script>
         axios.defaults.timeout=3000;
         axios.defaults.baseURL='http://localhost:3000/';
         axios.defaults.headers['mytoken']='aqwerwqwerqwer2ewrwe23eresdf23';
         axios.get('data').then(function(data){
             console.log(data.data);
         })
         
     </script>
</body>
</html>

相关文章

网友评论

      本文标题:axios API的基本使用方法

      本文链接:https://www.haomeiwen.com/subject/kjlpedtx.html