首页 > 其他分享 >Axios

Axios

时间:2023-02-08 15:00:36浏览次数:46  
标签:axios console log url res Axios config

Axios

Axios 是一个基于 promise 网络请求库,作用于node.js 和浏览器中。 它是 isomorphic 的(即同一套代码可以运行在浏览器和node.js中)。在服务端它使用原生 node.js http 模块, 而在客户端 (浏览端) 则使用 XMLHttpRequests。

服务器端

使用json-server

1. axios基本使用

// 1.GET
axios({
    method: 'GET',
    url: 'http://localhost:3000/posts/2'
}).then(res => {
    console.log(res);
})

// POST
axios({
    method: 'POST',
    url: 'http://localhost:3000/posts',
    data: {
        title: 'test',
        author: 'lll'
   }
}).then(res => {
    console.log(res);
})

// PUT
axios({
   method: 'PUT',
    url: 'http://localhost:3000/posts/3',
    data: {
        title: 'test',
        author: 'new-lll'
    }
}).then(res => {
    console.log(res);
})

// DELETE
axios({
    method: 'DELETE',
    url: 'http://localhost:3000/posts/3',
}).then(res => {
    console.log(res);
})

2. 其他请求方法

axios.request(config)
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.options(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])

// request
axios.request({
    method: 'GET',
    url: 'http://localhost:3000/posts/2'
}).then(res => {
    console.log(res)
})

// POST
axios.post(
    'http://localhost:3000/comments',
    {
        "body": "other",
       "postId": 2
    }
).then(res => {
    console.log(res)
})

3. axios默认配置

// default setting
axios.defaults.method = 'GET'
axios.defaults.baseURL = 'http:localhost:3000'
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

4. axios创建实例对象

// 创建实例对象
const obj = axios.create({
    baseURL: 'http://localhost:3000'
})
// obj实例和axios对象几乎相同
obj({
   url: 'posts/2'
}).then(res => {
    console.log(res)
})

5. axios拦截器

/**
 * 拦截器实质是函数
 * 请求拦截器,在请求发出时检查请求的参数等是否正确
 * 响应拦截器,在接受响应前,对响应进行预处理
*/
// 请求拦截器
axios.interceptors.request.use(functio(config) {
    console.log('req success')
    return config
}), function (error) {
    console.log('req fail')
    return Promise.reject(error)
}

// 接收拦截器
axios.interceptors.response.use(functio(response) {
    console.log('res success')
    return response;
}, function (error) {
    console.log('res fail')
    return Promise.reject(error);
});

6. 取消请求

let cancel = nul
btns[0].onclick = function () {
    // 检查上一个请求是否结束
    if (cancel !== null) {
        cancel()
    }
    axios({
        url: '/posts',
        cancelToken: new axios.CancelTok(function executor(c) {
            cancel = c;
        })
    }).then(res => {
        cancel = null
        console.log(res)
    })
btns[1].onclick = function () {
    cancel()
}

标签:axios,console,log,url,res,Axios,config
From: https://www.cnblogs.com/ucbb/p/17101771.html

相关文章

  • axios二次封装,mock前端模拟后端接口
    axios二次封装封装request,然后不用每次遇到接口就使用axios进行调用接口。封装一个基地址,然后每次调用接口的时候,只用写出来自己的函数方法就好。我们基于脚手架进行封装......
  • axios 请求
    完整文档官网文档html<inputtype="file"multipleid="files"><buttononclick="postAxios()">上传文件s</button><pid="files_progress"style="width:0px;heig......
  • axios封装+根据环境请求不同的url
    封装axios的时候,会创建一个axios实例,然后配置他的baseUrl,今天搞了好久才搞定,特来记录下首先安装axiosnpminstallaxios然后新建文件夹utils,建个js文件,用来封装axio......
  • 什么是axios
    Axios是专注于网络数据请求的库。相比于原生的XMLHttpRequest对象,axios简单易用。相比于jQuery,axios更加轻量化,只专注于网络数据请求axios发起get请求的语法:......
  • 书城8 - Vue&Axios
    1.Vue{{}}-相当于innerTextv-bind:attr绑定属性值。例如,v-bind:value-绑定value值简写::valuev-model双向绑定v-model:value,简写v-modelv-if,v-......
  • TS 封装 Axios
    前言Axios的二次封装是一项基础工作,主要目的就是将一些常用的功能进行封装,简化后续网络请求的发送。JS版本的封装大家都已经非常熟悉了,可以信手拈来。但是使用 TypeSc......
  • Axios的使用
    导入axios.js的脚本:<scriptsrc="js/axios.js"></script>对于get方式:直接在url后面添加参数和值<script>axios({method:"get",url:"http://loc......
  • Axios发送AJAX请求(通用方法型ajax)
    视频HTML<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><t......
  • Vue+axios+Servlet上传并显示图片
    做了和重写summernote插入图片的回调函数并上传图片到服务器一样的事,但是servlet简介:summernote点击上传(或粘贴)图片,前端用axios以multipart/form-data的形式传到后端,servl......
  • Axios快速入门
    参考目录json-server:https://www.cnblogs.com/fly_dragon/p/9150732.html安装json-server新建文件夹json-server,使用cmd在目录下使用命令npminstall-gjson-server......