ios ajax timeout无效,ajax - Express.js and axios route timeout - Stack Overflow

作者在使用Express创建私人项目时,遇到一个GET路由执行时间过长(约3分钟),正常浏览器访问显示正常,但通过axios的Ajax请求却反复触发,仅收到'Start'日志。尝试了setTimeout和增大超时时间,问题依旧。寻求解决此轮询问题的方法。
摘要由CSDN通过智能技术生成

I'm using express.js to generates some route for a private project. This particular route take a very long time to execute (about 3minutes). Since this is a project only for myself, I really don't mind waiting.

Here is my route:

router.get('/', (req, res) => {

req.setTimeout(0);

console.log('Start ' + new Date());

let data = [];

//... very large code that fetch a lots of data and update the data variable

res.json(data);

console.log('End ' + new Date());

});

If I'm executing the request directly in the browser url, the page take a long time to load, but after a while, will display the result correctly.

But if I'm using axios to make an ajax request like so

axios({

method: 'get',

url: `/api/simulation?idSession=${this.props.match.params.session}`,

timeout: 600000,

}).then(result => {

this.setState({

simulation: {

loading: false,

data: result.data

}

}).catch(err => alert(err));

});

I will receive the first console.log with the word 'Start' and the date, and pretty much exactly 2minutes after that (more or less some milliseconds), I will again receive the 'Start' console.log, and again.

I will receive the 'Start' console log in a loop until I restart the node server or close the browser's page with the ajax request.

I will still see the 'End' console.log after that, but axios seem to ignore it.

I though that by adding the req.setTimeout(0); in my route, it could solve the problem, but it doesn't seem to change anything.

And since the problem only occurs when I call the route via an ajax request with axios, I tried to put a large timeout, but that also did nothing.

Just in case, my nodejs version is 8.11.2, express version is 4.16.3 and axios is 0.18.0

Anyone has an other idea, or a way to disable all timeout?

Thanks

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值