javascript 为 ServerXMLHTTP 请求设置超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3927825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:43:03  来源:igfitidea点击:

Setting a timeout for ServerXMLHTTP request

asp-classictimeoutjavascriptserverxmlhttp

提问by Choy

Does anyone know how to set up set up a default action for when a ServerXMLHTTP request times out? I'm using setTimeouts() to set the time out options according to the MSDN site.

有谁知道如何为 ServerXMLHTTP 请求超时设置默认操作?我正在使用 setTimeouts() 根据MSDN 站点设置超时选项。

Ideally I would like to initialize the request again from the beginning or refresh the page should it time out.

理想情况下,我想从头开始再次初始化请求,或者在超时时刷新页面。

I'm using classic asp and jscript.

我正在使用经典的 asp 和 jscript。

Here's my request:

这是我的要求:

function serverXmlHttp(url) {
    var serverXmlHttp;
    serverXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP.6.0");

    // set time out options
    serverXmlHttp.setTimeouts(15000,15000,15000,15000);

    // does not work
    // serverXmlHttp.ontimeout(Response.Write("page has timed out"));

    serverXmlHttp.open("GET", url, false);
    serverXmlHttp.send();

    if (serverXmlHttp.readyState == 4) {
        return serverXmlHttp.responseText;
    }
}

采纳答案by Choy

Figured it out. I just need to use a try/catch statement.

弄清楚了。我只需要使用 try/catch 语句。

function serverXmlHttp(url) {

    try {
        var serverXmlHttp;
        serverXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP.6.0");

        // set time out options
        serverXmlHttp.setTimeouts(15000,15000,15000,15000);

        serverXmlHttp.open("GET", url, false);
        serverXmlHttp.send();

        if (serverXmlHttp.readyState == 4) {
            return serverXmlHttp.responseText;
        }
    catch(error) {
        // whatever I want to happen if there's an error
        Response.Write("Sorry, your request has timed out");
    }

}

回答by Gabriele Petrioli

The important thing is to find out why it is timing out ..

重要的是找出它超时的原因..

Is the remote Url on the same application as the calling page ?

远程 URL 是否与调用页面位于同一应用程序上?

if so have a look at INFO: Do Not Send ServerXMLHTTP or WinHTTP Requests to the Same Serveras you will be facing thread starvation ..

如果是,请查看信息:不要将 ServerXMLHTTP 或 WinHTTP 请求发送到同一台服务器,因为您将面临线程饥饿..