【干货满满】Axios用法大全,前端请求从此不再难!
发布人:shili8
发布时间:2024-11-17 02:37
阅读次数:0
**干货满满! Axios 用法大全**
前言:
在前端开发中,Ajax 请求是必不可少的。然而,使用 Ajax 时,我们经常会遇到一些问题,如请求方式、参数传递、数据格式等。Axios 是一个非常流行的 JavaScript 库,它可以帮助我们更轻松地进行 Ajax 请求。下面,我们将详细介绍 Axios 的用法大全。
**一、安装和引入**
首先,我们需要安装 Axios 库。使用 npm 或 yarn 安装:
bashnpm install axios
或者:
bashyarn add axios
然后,在你的 JavaScript 文件中引入 Axios:
javascriptimport axios from 'axios';
**二、基本请求**
Axios 的基本请求方式如下:
javascriptaxios.get('/api/user')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
在上面的例子中,我们使用 `get` 方法发送一个 GET 请求到 `/api/user` 地址。然后,Axios 会将响应数据传递给 `response` 对象中的 `data` 属性。
**三、请求参数**
我们可以通过 `params` 参数传递请求参数:
javascriptaxios.get('/api/user', {
params: {
id:1,
name: 'John'
}
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
在上面的例子中,我们传递了两个参数 `id` 和 `name`。
**四、请求头**
我们可以通过 `headers` 参数设置请求头:
javascriptaxios.get('/api/user', {
headers: {
'Content-Type': 'application/json'
}
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
在上面的例子中,我们设置了 `Content-Type` 头为 `application/json`。
**五、POST 请求**
我们可以使用 `post` 方法发送 POST 请求:
javascriptaxios.post('/api/user', {
name: 'John',
age:30})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
在上面的例子中,我们传递了一个 JSON 对象作为请求体。
**六、PUT 请求**
我们可以使用 `put` 方法发送 PUT 请求:
javascriptaxios.put('/api/user/1', {
name: 'Jane',
age:25})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
在上面的例子中,我们传递了一个 JSON 对象作为请求体。
**七、DELETE 请求**
我们可以使用 `delete` 方法发送 DELETE 请求:
javascriptaxios.delete('/api/user/1')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
在上面的例子中,我们传递了一个 URL 作为请求地址。
**八、错误处理**
我们可以使用 `try` `catch` 块来捕捉 Axios 的错误:
javascripttry {
axios.get('/api/user')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
} catch (error) {
console.error(error);
}
在上面的例子中,我们使用 `try` `catch` 块捕捉 Axios 的错误。
**九、取消请求**
我们可以使用 `cancelToken` 来取消 Axios 的请求:
javascriptconst source = axios.CancelToken.source();
axios.get('/api/user', {
cancelToken: source.token})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
source.cancel();
在上面的例子中,我们使用 `cancelToken` 来取消 Axios 的请求。
**十、Axios 配置**
我们可以使用 `axios.create()` 方法来创建一个新的 Axios 实例:
javascriptconst instance = axios.create({
baseURL: ' /> timeout:10000,
});
在上面的例子中,我们创建了一个新的 Axios 实例,并设置了 `baseURL` 和 `timeout`。
**十一、Axios 中的 Cookie**
我们可以使用 `withCredentials` 参数来传递 Cookie:
javascriptaxios.get('/api/user', {
withCredentials: true,
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
在上面的例子中,我们传递了 `withCredentials` 参数来传递 Cookie。
**十二、Axios 中的 JSONP**
我们可以使用 `jsonp` 参数来传递 JSONP:
javascriptaxios.get('/api/user', {
jsonp: 'callback',
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
在上面的例子中,我们传递了 `jsonp` 参数来传递 JSONP。
**十三、Axios 中的 Proxy**
我们可以使用 `proxy` 参数来设置代理:
javascriptaxios.get('/api/user', {
proxy: ' />})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
在上面的例子中,我们传递了 `proxy` 参数来设置代理。
**十四、Axios 中的 Transform**
我们可以使用 `transformRequest` 和 `transformResponse` 函数来转换请求和响应:
javascriptaxios.get('/api/user', {
transformRequest: [(data) => JSON.stringify(data)],
transformResponse: [(data) => JSON.parse(data)],
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
在上面的例子中,我们使用 `transformRequest` 和 `transformResponse` 函数来转换请求和响应。
**十五、Axios 中的 Cancel**
我们可以使用 `cancelToken` 来取消 Axios 的请求:
javascriptconst source = axios.CancelToken.source();
axios.get('/api/user', {
cancelToken: source.token,
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
source.cancel();
在上面的例子中,我们使用 `cancelToken` 来取消 Axios 的请求。
**十六、Axios 中的 Interceptors**
我们可以使用 `interceptors` 参数来设置拦截器:
javascriptaxios.interceptors.push({
request: (config) => {
console.log('Request:', config);
return config;
},
response: (response) => {
console.log('Response:', response);
return response;
},
});
在上面的例子中,我们设置了一个拦截器来打印请求和响应。
**十七、Axios 中的 Adapter**
我们可以使用 `adapter` 参数来设置适配器:
javascriptaxios.adapter = (config) => {
console.log('Adapter:', config);
return config;
};
在上面的例子中,我们设置了一个适配器来打印配置。
**十八、Axios 中的 Cookie Jar**
我们可以使用 `cookieJar` 参数来设置 cookie jar:
javascriptaxios.cookieJar = (jar) => {
console.log('Cookie Jar:', jar);
};
在上面的例子中,我们设置了一个 cookie jar 来打印 cookie。
**十九、Axios 中的 JSONP**
我们可以使用 `jsonp` 参数来传递 JSONP:
javascriptaxios.jsonp = (callback) => {
console.log('JSONP:', callback);
};
在上面的例子中,我们设置了一个 JSONP 来打印回调函数。
**二十、Axios 中的 Proxy**
我们可以使用 `proxy` 参数来设置代理:
javascriptaxios.proxy = (proxy) => {
console.log('Proxy:', proxy);
};
在上面的例子中,我们设置了一个代理来打印代理地址。
**二十一、Axios 中的 Transform**
我们可以使用 `transformRequest` 和 `transformResponse` 函数来转换请求和响应:
javascriptaxios.transformRequest = (data) => {
console.log('Transform Request:', data);
return data;
};
axios.transformResponse = (data) => {
console.log('Transform Response:', data);
return data;
};
在上面的例子中,我们设置了一个转换函数来打印请求和响应。
**二十二、Axios 中的 Cancel**
我们可以使用 `cancelToken` 来取消 Axios 的请求:
javascriptaxios.cancelToken = (token) => {
console.log('Cancel Token:', token);
};
在上面的例子中,我们设置了一个 cancel token 来打印 cancel token。
**二十三、Axios 中的 Interceptors**
我们可以使用 `inter

