Javascript Axios完成事件
我正在将Axios用于API服务,只是想知道是否有任何官方方法来处理Ajax调用中使用的“完整”事件 很像Javascript Axios完成事件,javascript,axios,Javascript,Axios,我正在将Axios用于API服务,只是想知道是否有任何官方方法来处理Ajax调用中使用的“完整”事件 很像 axios.get('/v1/api_endpoint?parameters') .then((res) => { .. }) .catch((err) => { .. }) .complete(() => {}) // <== is there any way to handle this complete event? axios.ge
axios.get('/v1/api_endpoint?parameters')
.then((res) => { .. })
.catch((err) => { .. })
.complete(() => {}) // <== is there any way to handle this complete event?
axios.get('/v1/api_endpoint?parameters'))
.然后((res)=>{..})
.catch((err)=>{..})
.complete(()=>{})/如果必须检查API调用是否成功,则可以使用以下代码:
const response = await axios.post(
"http://localhost:8000/xyz",
{ token, user }
);
const status = response.status
if (status == 200) {
console.log('Success')
toast("Successful Transaction", { type: "success" });
} else {
console.log('Falure')
toast("Falure", { type: "error" });
}
您还可以使用finally
检查事件是否完成。附上链接供您参考:
在axios文档之后,第二个.then()就是我要找的
下面是一个很好的示例,说明如何处理axios complete事件,无论成功与否,该事件都将始终执行
axios.get('/v1/api_endpoint?with_parameters')
.then((res) => { // handle success })
.catch((err) => { // handle error })
.then(() => { // always executed }) <-- this is the one
axios.get('/v1/api_endpoint?带_参数')
.then((res)=>{//handle success})
.catch((err)=>{//handle error})
.然后(()=>{//always executed})