玄子Share-Axios 零基础到手撕源码

玄子Share-Axios 零基础到手撕源码

Axios 简介

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

Axios 入门

使用方式

使用npm

$ npm install axios

1

使用CDN

1

模拟数据

官网:typicode https://github.com/typicode/json-server

安装 JSON 服务

npm install -g json-server

1

创建包含数据的db.json文件

{

"posts": [

{ "id": 1, "title": "json-server", "author": "typicode" }

],

"comments": [

{ "id": 1, "body": "some comment", "postId": 1 }

],

"profile": { "name": "typicode" }

}

1

2

3

4

5

6

7

8

9

启动 JSON 服务

json-server --watch db.json

1

请求http://localhost:3000/posts/1

{ "id": 1, "title": "json-server", "author": "typicode" }

1

关于更多描述以及路由请求见官网

Axios API

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]])

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

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

axios.patchForm(url[, data[, config]])
Title

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

Axios 实战

发送请求
Title

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

响应结构

一个请求的响应包含以下信息

{

// data 由服务器提供的响应

data: {},

// status 来自服务器响应的 HTTP 状态码

status: 200,

// statusText 来自服务器响应的 HTTP 状态信息

statusText: 'OK',

// headers 是服务器响应头

// 所有的 header 名称都是小写,而且可以使用方括号语法访问

// 例如: response.headers['content-type']

headers: {},

// configaxios 请求的配置信息

config: {},

// request 是生成此响应的请求

// 在node.js中它是最后一个ClientRequest实例 (in redirects),

// 在浏览器中则是 XMLHttpRequest 实例

request: {}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

并发请求
Title

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

相关推荐
补三补四14 小时前
html基本结构和常见元素
html5
大模型铲屎官1 天前
HTML常见文本标签解析:从基础到进阶的全面指南
前端·css·html·编程·html5·文本标签
补三补四1 天前
html的字符实体和颜色表示
前端·html5
山禾女鬼0012 天前
深入探索 HTML5 拖拽效果 API:打造流畅交互体验
前端·html·html5
学问小小谢3 天前
第23节课:前端调试技巧—掌握浏览器开发者工具与性能优化
前端·学习·安全·性能优化·交互·html5
酷爱码3 天前
HTML5+SVG+CSS3实现雪中点亮的圣诞树动画效果源码
前端·css3·html5
小韩学长yyds3 天前
前端实战:小程序搭建商品购物全流程
javascript·css·vue.js·小程序·前端框架·node.js·html5
红虾程序员4 天前
HTML一般标签和自闭合标签介绍
前端·pycharm·html·intellij-idea·html5
大模型铲屎官5 天前
从零开始学 HTML:构建网页的基本框架与技巧
前端·css·html·css3·html5·网页构建
Lorcian6 天前
web前端11--伪类与过渡
前端·css·笔记·html5·visual studio code