玄子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

相关推荐
Amumu121389 小时前
HTML5的新特性
前端·html·html5
橘子编程9 小时前
OpenClaw(小龙虾)完整知识汇总
java·前端·spring boot·spring·spring cloud·html5
酉鬼女又兒10 小时前
零基础快速入门前端蓝桥杯Web考点深度解析:var、let、const与事件绑定实战(可用于备赛蓝桥杯Web应用开发)
开发语言·前端·javascript·职场和发展·蓝桥杯·es6·html5
我命由我1234515 小时前
HTML 开发 - HTML 描述列表标签(<dl>、<dt>、<dd>)
前端·javascript·css·html·css3·html5·js
我命由我123451 天前
React - 类组件 setState 的 2 种写法、LazyLoad、useState
前端·javascript·react.js·html·ecmascript·html5·js
spencer_tseng2 天前
HTML5 - Android - IOS
android·ios·html·html5
我命由我123453 天前
React - React Redux 数据共享、Redux DevTools、React Redux 最终优化
前端·javascript·react.js·前端框架·ecmascript·html5·js
深邃-4 天前
字符函数和字符串函数(1)
c语言·开发语言·数据结构·c++·算法·html5
我命由我123454 天前
Vite - Vite 最小项目
服务器·前端·javascript·react.js·ecmascript·html5·js
橘子编程4 天前
HTML5 权威指南:从入门到精通
前端·css·vue.js·html·html5