自定义 axios
自定义 axios
function axios({
url,
method = 'GET',
params = {},
data = {}
}) {
// 返回一个 promise 对象
return new Promise((resolve, reject) => {
// 处理 query 参数(拼接到url上) id=1&xxx=abc
// {id: 1, xxx: 'abc'}
let queryString = ''
Object.keys(params).forEach(key => {
queryString += `${key}=${params[key]}&`
})
if (queryString) {
// 取出最后的 &
queryString = queryString.substring(0, queryString.length - 1)
// 连接到 url
url += '?' + queryString
}
// 1.执行异步 ajax 请求
// 创建 xhr 对象
const request = new XMLHttpRequest()
// 打开连接(初始化请求,还没有请求)
request.open(method, url, true)
// 发送请求
if (method === 'GET' || method === 'DELETE') {
request.send()
} else if (method === 'POST' || method === 'PUT') {
// 告诉服务器请求的格式是 json
request.setRequestHeader('Content-Type', 'application/json;chaset=utf-8')
// 发送 json 格式请求体数据
request.send(JSON.stringify(data))
}
// 绑定状态改变的监听
request.onreadystatechange = function() {
// 如果请求没有完成,直接结束
if (request.readyState !== 4) {
return
}
// 如果状态码在 [200, 300) 范围内表示成功,否则失败
const {status, statusText} = request
// 2.1 如果请求成功了,调用 resolve()
if (status <= 200 && status < 300) {
// 准备结果数据对象 response
const response = {
data: JSON.parse(request.response),
status,
statusText
}
resolve(response)
} else { // 2.2 如果请求失败了,调用 reject()
reject(new Error('request error status is ' + status + ',' + statusText))
}
}
})
}
自定义 axios的更多相关文章
- HTTP库Axios
前面的话 本文将详细介绍HTTP库Axios 概述 Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中 [安装] 在Vue中使用,最好安装两个模块axios ...
- axios介绍与使用说明 axios中文文档
本周在做一个使用vuejs的前端项目,访问后端服务使用axios库,这里对照官方文档,简单记录下,也方便大家参考. Axios 是一个基于 Promise 的 HTTP 库,可以用在浏览器和 node ...
- Axios 使用文档
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 使用实例:http://www.cnblogs.com/coolslider/p/7838309.ht ...
- Axios 中文说明
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. Features 从浏览器中创建 XMLHttpRequests 从 node.js 创建 http ...
- Axios
axios 是一个基于Promise 用于浏览器和 nodejs 的 HTTP 客户端.简单的理解就是ajax的封装 它本身具有以下特征: a.从浏览器中创建 XMLHttpRequest b ...
- 一个axios的简单教程
转载于:https://www.jianshu.com/p/13cf01cdb81f 转载仅供个人学习 首先要明白的是axios是什么:axios是基于promise(诺言)用于浏览器和node.js ...
- axios,vue-axios在项目中的应用
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中 关于axios的功能: 1,从浏览器中创建XMLHttpRequests 2,从node.js常见Htt ...
- axios中文文档
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. Features 从浏览器中创建 XMLHttpRequests 从 node.js 创建 http ...
- Axios的详细配置和相关使用
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. Features 从浏览器中创建 XMLHttpRequests 从 node.js 创建 http ...
随机推荐
- three.js 添加html内容、文本
需求: 1.在场景内添加html元素并动态更新 2.html内容需跟随场景变化 方案: 新加方案:https://www.zhihu.com/question/49929467/answer/1186 ...
- postgresql批量插入copy_from()的使用
在批量插入postgresql时想使用同Mysql的语法时发现并不能使用: cursor.executemany("INSERT INTO persons VALUES (%d, %s, % ...
- CF1556D-Take a Guess【交互】
正题 题目链接:https://codeforces.com/contest/1556/problem/D 题目大意 现在有\(n\)个你不知道的数字,你有两种询问操作 询问两个下标的数字的\(and ...
- JPA基本用法
jpa基本查询 1.继承JpaRepository,生成了CRUD方法 public void testBaseQuery() throws Exception { User user=new U ...
- 10.13 Nginx 负载均衡
七层负载均衡server { listen 80; server_name localhost; location / { proxy_pass http://name; //调用集群 } } ups ...
- C++核心编程 4 类和对象-封装
C++面向对象的三大特性:封装.继承.多态 C++认为万事万物皆为对象,对象上有其属性和行为 封装 意义:1.将属性和行为作为一个整体,表现生活中的事物 语法: class 类名{ 访问权限:属性 ...
- 题解 [ZJOI2016]大森林
题目传送门 Description 现在有 \(n\) 棵以 \(1\) 为根的树,每棵树有一个生长节点,有 \(m\) 次操作,每次操作是下面三种中的一个: 在 \(l\sim r\) 的这些树的生 ...
- 洛谷3783 SDOI2017 天才黑客(最短路+虚树+边转点+线段树优化建图)
成功又一次自闭了 怕不是猪国杀之后最自闭的一次 一看到最短路径. 我们就能推测这应该是个最短路题 现在考虑怎么建图 根据题目的意思,我们可以发现,在本题中,边与边之间存在一些转换关系,但是点与点之间并 ...
- Vulnstack内网靶场2
环境配置 内网2靶场由三台机器构成:WIN7.2008 server.2012 server 其中2008做为对外的web机,win7作为个人主机可上网,2012作为域控 网络适配器已经设置好了不用自 ...
- 2.3 Core Building Blocks 核心构件
Core Building Blocks 核心构件 DDD mostly focuses on the Domain & Application Layers and ignores the ...