首页 > 其他分享 >axios

axios

时间:2022-10-07 20:26:51浏览次数:59  
标签:function axios return 请求 get response

学习文档:https://www.jianshu.com/p/9359bf779376

Promise:https://blog.csdn.net/z591102/article/details/108510315

axios是基于promise管理ajax请求库

安装:
利用yarn安装 : yarn add axios,如果显示yarn没有找到该命令,则需要安装npm install -g yarn
利用npm安装 : npm install axios --save
利用bower安装 : bower install axios --save
利用cdn引入 : <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

实例全局配置

// npm i axios
import axios from 'axios'
const http = axios.create({
baseURL: process.env.VUE_API_APP_URL,
timeout: 1000,
})

axios常用请求方法
get 获取数据
post 提交数据
put 更新数据(所有数据推送到后端)
patch 更新数据(只将修改的数据推送到后端)
delete 删除数据

GET

axios.get('/data.json',{
// 注 此处参数写入params中 ---get请求需要用到params
params: {
id: 'zxm'
}
}).then(res => {
console.log(res);
})

 

POST

// post:参数直接跟在url后面即可
axios.post('xxxxxxxxx', {
xxx: 'xxxx',
xxxx: 'xxxx'
}
}).then(res => {
console.log(res);
})

 

put请求 ---上面get,post为简单写法

axios({
method: 'PUT',
url: " http://localhost:3000/persons/1",
data: {
name:'dyk12',
age:8
}
}).then((response) => {
console.log(response.data)
});

 

delete请求

axios({
method: 'DELETE',
url: "http://localhost:3000/persons/1",
}).then((response) => {
console.log(response.data)
});

 

并发请求:

function getUserAccount() {
return axios.get('/user/12345');
}

function getUserPermissions() {
return axios.get('/user/12345/permissions');
}

axios.all([getUserAccount(), getUserPermissions()])
.then(axios.spread(function (acct, perms) {
// 两个请求现在都执行完成
}));

 

拦截器

// 添加请求拦截器
const reqInterceptor = axios.interceptors.request.use(function (config) {
return config;
}, function (error) {
return Promise.reject(error);
});

// 添加响应拦截器
const resInterceptor = axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
return Promise.reject(error);
});
// 移除拦截器
axios.interceptors.request.eject(reqInterceptor);
axios.interceptors.request.eject(resInterceptor);

标签:function,axios,return,请求,get,response
From: https://www.cnblogs.com/xinyu-yudian/p/16760587.html

相关文章

  • 前端Axios-Day44
    JSONServer:模拟服务器环境插件1.进行全局安装:npmi-gjson-server2.创建db.json文件并写入相关数据:{"posts":[{"id":1,"title":"json-server","author......
  • 同时多个axios请求怎么实现无痛刷新token
    需求最近遇到个需求:前端登录后,后端返回token和token有效时间,当token过期时要求用旧token去获取新的token,前端需要做到无痛刷新token,即请求刷新token时要做到用户无感知。......
  • h5:vue3 + ts + vite + vuex + axios + vant4 + scss + postcss+mockjs+element-plus
    模板地址:https://gitee.com/zhang_meng_lei/mobile-template-h5-vue3/tree/master安装element-plus:yarnaddelement-plus(目前已导入但未实现代码)按需导入:https://el......
  • 关于Axios传json对象给后端,后端将json在转换为pojo对象,
    Controller使用@ResquestParam注解,形参并不直接写pojo对象,而是Map<String,Object>对象,然后使用其get(“key”)方法得到前端作为url参数传递过来的json格式的object对象,使用......
  • JavaWeb----Vue+Axios+Json的数据请求
    详细资料:https://heavy_code_industry.gitee.io/code_heavy_industry/pro001-javaweb/lecture/chapter12/verse02.html《axios+vue发送普通参数请求》  《Json》 ......
  • Fetch and axios
       ......
  • node js post请求(axios)
    node.js可以直接运行js文件 在电脑上配置了node就可以直接用了运行js文件:用vscode打开终端运行语法:nodefilename.js例:  node自带http模块,可以直接写post请......
  • axios请求配置
    importaxiosfrom'axios'importqsfrom'qs'axios.defaults.headers['Content-Type']='application/x-www-form-urlencoded;charset=UTF-8'//'Content-Type':'a......
  • vue3+vant+vue-router+axios+pinia+vite框架搭建
    vue3的官网地址:https://cn.vuejs.org/;这里要说一下,vue3不支持IE11,如果要兼容IE11及其一下,不建议使用vue3。创建vue脚手架,如果你需要使用ts,则需要node版本>=16。本文按照......
  • axios学习笔记
     一.  安装json-server 01安装npminstall-gjson-serverhttps://github.com/typicode/json-server 02,新建一个db.json文件,把上面链接文档的数据放上去......