如何在 Vue.js 和 Webpack 中使用 axios 设置 GET 请求的超时?例如我有这个代码
axios.get("myURL").then(function(response) {
// code
}
Run Code Online (Sandbox Code Playgroud)
我尝试包含 {timeout:3000} 作为参数,但它不起作用
据我了解,timeout
axios中的 是 a response timeout
,而不是connect timeout
. 您可以尝试使用该cancelToken
功能
const connectToServer = ip => {
let source = CancelToken.source();
setTimeout(() => {
source.cancel();
}, 3000);
return axios.get(ip + '/config', {cancelToken: source.token}).then((result) => {
// My logic
})
};
Run Code Online (Sandbox Code Playgroud)
这将始终取消,或者如果您只想在没有响应时取消:
function loginButtonPressed(username, password) {
return async (dispatch) => {
const source = CancelToken.source();
try {
let response = null;
setTimeout(() => {
if (response === null) {
source.cancel();
}
}, 4000);
dispatch(authenticationPending());
response = await axios.post('auth/login',
{username, password},
{cancelToken: source.token});
dispatch(authenticationSuccess());
} catch (error) {
dispatch(authenticationFailed());
}
};
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14877 次 |
最近记录: |