Axios只在内部自调用函数中调用一次(Internet Explorer) [英] Axios only called once inside self-invoking function (Internet Explorer)

查看:379
本文介绍了Axios只在内部自调用函数中调用一次(Internet Explorer)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个每2.5秒调用一次的函数来检查在后台运行的任务。如果响应是错误,它会将axios调用 get 一个url,如果响应成功,我会停止该函数。

I have a function that calls itself every 2.5 seconds to check on a task running in the background. It calls axios to get a url if the response is an error, and if the response is successful, I stop the function.

这适用于Chrome和Mozilla,但出于某种原因,它在IE(版本11)中不起作用。该函数无限调用自己,但检查日志显示它只调用一次axios,然后它有 res.data.err ==任务未就绪永远循环,甚至如果后端的任务已经完成。

This works perfectly on Chrome and in Mozilla but for some reason it doesn't work in IE (version 11). The function calls itself infinitely, but checking the logs show that it only calls axios once, then it has res.data.err == "Task not ready" looping forever, even if the task on back-end has already finished.

为什么IE上没有再次调用axios?有什么我想念的吗?

Why isn't axios being invoked again on IE? Is there anything I'm missing?

checkProgress() { 
    axios.get(url.CHECK_UPLOAD_TASK, { params: { id: this.state.taskID} }).then(res => {
        if(res.data.success){
            this.setState({loading: false});
            //do something if successfully finished task
        } else {
            if(res.data.err == "Task not ready") {
                setTimeout(() => {
                    this.checkProgress();
                },2500);
            } else {
                this.setState({loading: false});
                // do something if task had an error
            }
        }
    });
}


推荐答案

我遇到过类似的问题(尽管使用fetch)也可以在InternetExplorer(11,Edge)上轮询服务。我发现使用Pragma:no-cache标题解决了它。

I have encountered a similar issue (although using fetch) on InternetExplorer as well (11, Edge) for polling a service. I found that using the "Pragma: no-cache" header solved it.

你可以尝试一下:

const config = {
    headers: { Pragma: 'no-cache'},
    params: { id: this.state.taskID }
}

看涨期权如下:

axios.get(url.CHECK_UPLOAD_TASK, config).then(...)

这篇关于Axios只在内部自调用函数中调用一次(Internet Explorer)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆