我在Web应用程序中使用axios发出了GET请求:
axios({
method: 'get',
url: "example.com",
params:{
_id: "anId"
}
})
.then((response) => {
//do stuffs
})
.catch((error) => {
// need params._id here ?!!
})
是否有可能在请求的错误处理部分中获取参数?
谢谢。
最佳答案
您可以关闭(关闭)参数以使其保持上下文:
function myWrapper() {
const params = {
a: 'a',
b: 'b'
}
axios({
method: 'get',
url: "example.com",
params: params
})
.then((response) => {
// you have access to params here
console.log(params.a);
//do stuffs
})
.catch((error) => {
// you have access to params here
console.log(params.a);
// need params._id here ?!!
})
}
注意,这未经测试,但应该可以完成
这是一个正在运行的示例:
function myWrapper() {
const params = {
a: 'a',
b: 'b'
}
axios({
method: 'get',
url: "example.com",
params: params
})
.then((response) => {
// you have access to params here
console.log(params.a);
//do stuffs
})
.catch((error) => {
// you have access to params here
console.log('error', error.message);
console.log('params!!', params);
// need params._id here ?!!
})
}
myWrapper();
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.17.1/axios.js"></script>