目录

一、axios使用

1.支持多种请求方式:

2.安装

3.简单使用实例

4.发送并发请求

5.全局配置

二、axios的实例

1.为什么要创建 axios的实例呢?

2.使用

三、、axios模块封装

四、axios拦截器


一、axios使用

1.支持多种请求方式:

axios(config)

axios.request(config)

axios.get(url[, config])

axios.delete(url[, config])

axios.head(url[, config])

axios.post(url[, data[, config]])

axios.put(url[, data[, config]])

axios.patch(url[, data[, config]])

2.安装

npm install axios --save

3.简单使用实例

1.安装
import axios from 'axios'
2.get请求
axios({// 默认是geturl: 'http://localhost:8000/home/multidata',method: 'get'
}).then((result) => {console.log(result)
})
---
axios({//url:  'http://localhost:8000/home/data?type=sell&page=1',url: 'http://localhost:8000/home/data',params: {type: 'sell',page: 1},method: 'get'
}).then((result) => {console.log(result)
})

4.发送并发请求

有时候,我们可能需求同时发送两个请求

使用axios.all,可以放入多个请求的数组。

axios.all([]) 返回的结果是一个数组,使用axios.spread可将数组[res1, res2]展开为res1,res2

axios.all([axios({url: 'http://localhost:8000/home/multidata'
}), axios({url: 'http://localhost:8000/home/data',params: {type: 'sell',page: 1}
})]).then((results) => {console.log(results)
})
//axios.spread展开(不太常用)
axios.all([axios({url: 'http://localhost:8000/home/multidata'
}), axios({url: 'http://localhost:8000/home/data',params: {type: 'sell',page: 1}
})]).then(axios.spread((res1, res2) => {console.log(res1)console.log(res2)
}))
//数组的解构,对应也有对象的解构{name, age} = obj
axios.all([axios({url: 'http://localhost:8000/home/multidata'
}), axios({url: 'http://localhost:8000/home/data',params: {type: 'sell',page: 1}
})]).then(([res1, res2]) => {console.log(res1)console.log(res2)
})

5.全局配置

(1)在上面的示例中,我们的BaseURL是固定的

事实上,在开发中可能很多参数都是固定的。

这个时候我们可以进行一些抽取,也可以利用axios的全局配置

axios.defaults.baseURL = 'http://localhost:8000'
axios.defaults.headers.post['Content-Type'] = 'application/x-www/form-urlencoded'
axios.default.timeOut = 50000axios.all([axios({url: '/home/multidata'
}), axios({url: '/home/data',params: {type: 'sell',page: 1}
})]).then((results) => {console.log(results)
})

(2)常见配置项

请求地址

url:'/user',

请求类型

method:'get',

请根路径

baseURL:'http://www.mt.com/apl,

请求前的数据处理

transformRequest: [function(data){}]

请求后的数据处理

transformResponse: [function(data){}],

自定义的请求头

headers:{'x-requested-With':'XMLHttpRequest'},

URL查询对象

params:{id:12},

查询对象序列化函数

paramsSerializer: function(params){}

request body

data:{key:'a'}

超时设置s

timeout: 1000,

跨域是否带 Token

withCredentials: false,

自定义请求处理

adapter: function(resolve, reject, config){},

身份验证信息

auth: {uname:", pwd: '12'},

响应的数据格式json/blob/ document/ arraybuffer/text/ stream

responseType:'json',

二、axios的实例

1.为什么要创建 axios的实例呢?

口当我们从 axios模块中导入对象时,使用的实例是默认的实例

口当给该实例设置一些默认配置时,这些配置就被固定下来了.

口但是后续开发中,某些配置可能会不太一样.

口比如某些请求需要使用特定的 baseURL或者 timeouti或者 content-Type等

口这个时候我们就可以创建新的实例,并且传入属于该实例的配置信息.

2.使用

const instance1 = axios.create({// 全局实例配置baseURL: 'http://localhost:8000',timeOut: 5000
})instance1({url: '/home/multidata'
}).then(res => {console.log(res)
})

三、、axios模块封装

import axios from 'axios'
// 1.传success、failure方法
export function request(config, success, failure) {//1.创建实例const instance = axios.create({baseURL: 'http://localhost:8000',timeOut: 5000})// 2.发送真正的网络请求instance(config).then(res => {success(res)}).catch(err => {failure(err)})
}
// 2.只传config
export function request2(config) {//1.创建实例const instance = axios.create({baseURL: 'http://localhost:8000',timeOut: 5000})// 2.发送真正的网络请求instance(config.baseConfig).then(res => {config.success(res)}).catch(err => {config.failure(err)})
}
// 3.使用Promise(axios本身是返回一个Promise,所以没必要再次返回一个promise了)
export function request3(config) {return new Promise((resolve, reject) => {//1.创建实例const instance = axios.create({baseURL: 'http://localhost:8000',timeOut: 5000})// 2.发送真正的网络请求instance(config).then(res => {resolve(res)}).catch(err => {reject(err)})})
}
// 4.简化
export function request4(config) {//1.创建实例const instance = axios.create({baseURL: 'http://localhost:8000',timeOut: 5000})// 2.发送真正的网络请求return instance(config);
}
// 封装网络请求
import {request, request2, request3, request4} from  './network/request'
request({url: '/home/multidata'
}, res => {console.log(res)
})
request2({baseConfig: {url: '/home/multidata'},success: function(res) {console.log(res)}
})
request3({url: '/home/multidata'
}).then(res => {console.log(res)
}).catch(err => {console.log(err)
})
request4({url: '/home/multidata'
}).then(res => {console.log(res)
})

四、axios拦截器

     //1.创建实例const instance = axios.create({baseURL: 'http://localhost:8000',timeOut: 5000})// 1.1拦截器,必须return,否则拿不到东西instance.interceptors.request.use(config => {console.log('req拦截器success')// 比如config中的一些信息不符合服务器的要求// 比如每次发送网络请求时,都希望在界面显示一个请求的图标// 某些网络请求(比如登录)必须携带一些特殊的东西console.log(config)return config}, err => {console.log('req拦截器fail')console.log(err)return err})instance.interceptors.response.use(response => {console.log('resp拦截器success中')return response.data}, err => {console.log('resp拦截器fail中')return err})// 2.发送真正的网络请求instance(config).then(res => {success(res)}).catch(err => {failure(err)})

vue如何发送网络请求,使用axios事半功倍!相关推荐

  1. 如何在UNIAPP里发送网络请求 #在axios里内嵌request的大聪明前来填坑

    目录 1)用axios封装,内嵌request

  2. Vue(五)Vue中的网络请求(使用Vue脚手架发送Axios请求)

    一.Axios         1.定义:利用ES6提供的Promise方式,把AJAX进行了封装.我们在Vue中发送网络请求,基本上就是使用Axios 需要安装第三方的Axios模块,才能使用    ...

  3. Fetch发送网络请求

    1. 文档 https://github.github.io/fetch/ https://segmentfault.com/a/1190000003810652 2. 特点 fetch: 原生函数, ...

  4. axios发送网络请求

    网络模块封装 选择什么网络模块 传统的Ajax 缺点:配置和调用方式等非常混乱 jQuery-Ajax 缺点:在Vue整个开发中都是不需要使用jQuery的 axios axios 功能特点: 在浏览 ...

  5. 网络编程-JavaScript中发送网络请求汇总

    文章目录 1.前后端分离优势 2.HTTP协议的解析 2.1 HTTP的介绍 2.2 HTTP的组成 2.3 HTTP的版本 2.4 HTTP请求方式 2.5 HTTP请求头字段 2.6 HTTP响应 ...

  6. 微信小程序中发送网络请求

    文章目录 小程序项目 app.json pages/index/index.html pages/index/index.wxss pages/index/index.js 发送网络请求 网络请求函数 ...

  7. OkHttp3 发送网络请求服务器

    前言:应用程序需要发送网络请求服务器的接口,可使用OkHttp 3发送请求获取服务端数据 GitHut地址 Step 1:申请网络请求的权限:在manifests层的AndroidManifest.x ...

  8. python 网络接口 开发_Python自动化学习笔记(八)——接口开发、发送网络请求、发送邮件、写日志...

    1.接口开发(flask模块) Python自动化学习笔记(七)接口开发部分的内容补充 1.1参数为json格式: flask.request.is_json #判断参数是否是json格式 flask ...

  9. Python自动化学习笔记(八)——接口开发、发送网络请求、发送邮件、写日志...

    1.接口开发(flask模块) Python自动化学习笔记(七)接口开发部分的内容补充 1.1参数为json格式: flask.request.is_json #判断参数是否是json格式 flask ...

最新文章

  1. TensorFlow人工智能引擎入门教程之二 CNN卷积神经网络的基本定义理解。
  2. Combotree--别样的构建层级json字符串
  3. 【线段树】开关(luogu 3870)
  4. Python 使用pdfplumber 提取PDF页面表格的内容
  5. java日期加一天_Java 关于日期加一天(日期往后多一天)
  6. 毕业10年,阻碍你职业发展的最大“拦路虎”到底是什么?
  7. opencv 访问图像的像素方法和算法用时的计算
  8. 计算机表格怎么往下排序,如何在Excel中随机排序表格中的顺序
  9. 大型央企云边协同建设方案及其借鉴意义分析
  10. 苹果4如何添加时间插件_iPhone如何添加输入法?苹果手机怎样添加手写输入?...
  11. 石头机器人拖地水量调节_用石头扫地机器人扫地拖地是一种什么体验
  12. 微信支付--网页版-V3-(2)
  13. 解决远程windows server12桌面复制文件大于2G出错问题
  14. Pyecharts绘图笔记
  15. 自动滑动背景图片html,html背景图片滚动属性bgproperties
  16. 关于CAJViewer阅读器如何修改做标记的颜色?
  17. wordpress如何访问网页时显示域名不显示ip
  18. 和大华电子称进行对数据通讯
  19. 客户订购登记管理系统(数据库)
  20. python12306买票_Python爬虫之12306-买票器小白源码

热门文章

  1. windows10下同时安装两个mysql服务的解决办法
  2. CSS中常用的样式语法
  3. 元气骑士里的超级计算机,元气骑士:本以为“素颜相机”够火,直到看到它,一秒笑出猪叫声...
  4. 安卓 sharedpreferences可以被其它activity读取_【安卓逆向】“一份礼物”之我要o泡逆向分析...
  5. windows服务又界面吗_详解远程桌面协议, Linux 和 Windows 间的远程桌面互相访问(RDP、VNC协议)...
  6. 电脑微信多开方法_微信电脑端多开方法
  7. QT判断该文件是否存在
  8. windbg检测句柄泄露(定位到具体代码)
  9. linux反调试代码,linux反调试方法
  10. java连服务器mysql报错_tomcat程序连接服务器mysql数据库报错