使用 Axios 等待服务器响应的时间
Waiting time for a response from server using Axios
我想使用如下命令插入新行:
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
我需要响应包含新创建的用户行的新 ID,该行包含用户 Fred Flintstone 的信息。但是,由于这是一个数据库,我的脚本可能需要等待更多时间。如何控制服务器响应的等待时间?也就是说
我该怎么做才能告诉 javascript Axios 命令在触发由于 "no response from the server" 导致的错误消息之前等待几秒钟。这可能吗?
axios
得到一个 timeout
属性 可以设置请求超时前的毫秒数。
axios({
method: 'post',
url: '/user',
timeout: 8000, // Let's say you want to wait at least 8 seconds
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
我想使用如下命令插入新行:
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
我需要响应包含新创建的用户行的新 ID,该行包含用户 Fred Flintstone 的信息。但是,由于这是一个数据库,我的脚本可能需要等待更多时间。如何控制服务器响应的等待时间?也就是说
我该怎么做才能告诉 javascript Axios 命令在触发由于 "no response from the server" 导致的错误消息之前等待几秒钟。这可能吗?
axios
得到一个 timeout
属性 可以设置请求超时前的毫秒数。
axios({
method: 'post',
url: '/user',
timeout: 8000, // Let's say you want to wait at least 8 seconds
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});