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

查看:34
本文介绍了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 == "Task not ready" 永远循环,即使后面的任务 -end 已经结束了.

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
            }
        }
    });
}

推荐答案

我在 InternetExplorer (11, Edge) 上也遇到了类似的问题(虽然使用 fetch)来轮询服务.我发现使用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天全站免登陆