axios

简介: axios

axios

基本使用

增删改查,get查,post增,put改,delete查

HTML

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
<body>
<buttonid="1">点我</button>
<buttonid="2">点我2</button>
<buttonid="3">点我3</button>
<buttonid="5">点我5</button>
<script>
var btn = document.getElementById('1')
var btn2 = document.getElementById('2')
var btn3 = document.getElementById('3')
var btn5 = document.getElementById('5')
         btn.onclick=function(){
            axios({
method:'GET',
url: 'http://localhost:3000/posts',
            }).then(response=>{
console.log(response)
            });
         }
         btn2.onclick=function(){
            axios({
method:'POST',
url: 'http://localhost:3000/posts',
data:{
title: "hello world",
author: "chenhao"
                }
            }).then(value=>{
console.log(value)
            },reason=>{
console.log(reason)
            });
         }
         btn3.onclick=function(){
            axios({
method:'delete',
url: 'http://localhost:3000/posts/3',
            }).then(value=>{
console.log(value)
            },reason=>{
console.log(reason)
            });
         }
         btn5.onclick=function(){
            axios({
method:'PUT',
url: 'http://localhost:3000/posts/2',
data:{
title: "hello world",
author: "libai"
                }
            }).then(value=>{
console.log(value)
            },reason=>{
console.log(reason)
            });
         }
</script>
</body>

默认配置

JS

1
2
axios.defaults.method='POST'
   axios.defaults.baseURL='http://localhost:3000'

拦截器

JS

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
//增加一个请求拦截器
axios.interceptors.request.use(function (config) {
// Do something before request is sent
console.log("请求拦截器成功")
return config;
  }, function (error) {
// Do something with request error
console.log("请求拦截器失败")
returnPromise.reject(error);
  });
//增加一个响应拦截器
axios.interceptors.response.use(function (response) {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
console.log("响应拦截器成功")
return response;
  }, function (error) {
// Any status codes that falls outside the range of 2xx cause this function to trigger
console.log("响应拦截器成功")
// Do something with response error
returnPromise.reject(error);
  });
axios({
method:'GET',
url: 'http://localhost:3000/posts'
})
目录
相关文章
|
前端开发
基于RuoYi-Flowable-Plus的若依ruoyi-nbcio支持自定义业务表单流程(二)
基于RuoYi-Flowable-Plus的若依ruoyi-nbcio支持自定义业务表单流程(二)
770 2
|
8月前
|
小程序 C语言
【C语言程序设计——基础】顺序结构程序设计(头歌实践教学平台习题)【合集】
目录 任务描述 相关知识 编程要求 测试说明 我的通关代码: 测试结果: 任务描述 相关知识 编程编写一个程序,从键盘输入3个变量的值,例如a=5,b=6,c=7,然后将3个变量的值进行交换,使得a=6,b=7,c=5。面积=sqrt(s(s−a)(s−b)(s−c)),s=(a+b+c)/2。使用输入函数获取半径,格式指示符与数据类型一致,实验一下,不一致会如何。根据提示,在右侧编辑器补充代码,计算并输出圆的周长和面积。
162 10
|
Python
全网最适合入门的面向对象编程教程:Python函数方法与接口-函数与方法的区别和lamda匿名函数
【9月更文挑战第15天】在 Python 中,函数与方法有所区别:函数是独立的代码块,可通过函数名直接调用,不依赖特定类或对象;方法则是与类或对象关联的函数,通常在类内部定义并通过对象调用。Lambda 函数是一种简洁的匿名函数定义方式,常用于简单的操作或作为其他函数的参数。根据需求,可选择使用函数、方法或 lambda 函数来实现代码逻辑。
130 7
|
11月前
【九度 OJ 08】简单查找x
【九度 OJ 08】简单查找x
60 0
|
开发框架 Dubbo 应用服务中间件
微服务开发框架-----Apache Dubbo
这篇文章介绍了Apache Dubbo微服务开发框架,它提供RPC通信和微服务治理能力,支持服务发现、负载均衡和流量治理等功能,并强调了Dubbo在微服务规模化实践和企业级治理方面的优势。
微服务开发框架-----Apache Dubbo
|
JSON NoSQL MongoDB
MongoDB详解(五)——MongoDB数据库简单使用
MongoDB详解(五)——MongoDB数据库简单使用
619 1
|
安全 Java Serverless
65w字!阿里分布式开发小册Github新开源!原理实践双飞
我们都知道传统的集中式系统已无法满足当今的互联网三高需求,所以现在的系统架构都是向着分布式系统不断演进。同时,越来越多的企业选择通过云的方式发布和部署应用,这也大大促进了分布式系统的发展。未来将是分布式系统“爆发”的时代。
|
存储 Serverless
使用R语言进行时间序列(arima,指数平滑)分析(上)
使用R语言进行时间序列(arima,指数平滑)分析
|
存储 Kubernetes API
听GPT 讲Istio源代码--pkg(11)
听GPT 讲Istio源代码--pkg(11)
106 0
|
微服务
SpringCloud - 微服务之多模块化(一)
SpringCloud - 微服务之多模块化(一)
870 0
SpringCloud - 微服务之多模块化(一)