weixin_33695082 2013-04-11 22:04 采纳率: 0%
浏览 17

AJAX:更多

For asynchronous quick-checks of an URL, I use AJAX with method=HEAD:

function ajax_quickresponse(url) {
    var xmlhttp = null;
    if (window.XMLHttpRequest)
        xmlhttp = new XMLHttpRequest();
    else if (window.ActiveXObject)
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    if (xmlhttp) {
        xmlhttp.open("HEAD", url, true);
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {
                if ((xmlhttp.status > 199 && xmlhttp.status < 400))
                    ; //OK
                else
                    ; //ERR
            };
        };
        xmlhttp.send(null);
    };
};

This receives is enough for checking the http status, but seems to abort the script at serverside (url). E.g. I can try with a simple php script (url="http://my-host.er/test/script.php") which does sleep(2); and log a success message afterwards. With xmlhttp.open("HEAD", url, true);, there is no success entry in the log.

With xmlhttp.open("GET", url, true);, there is a success entry in the log.

However, with GET/POST, the javascript is waiting 2 seconds instead of instantly (with HEAD).

The status is known instantly and the javascript does not need to wait for the final response.

How to take advantage of both methods? First, I need the status instantly, as soon as the header comes in, and after the external url/script returns, then i'd like another listener.

E.g. first alert('http status='+xmlhttp.status); and maybe delayed, depending on the url/script, alert('finally completed');

Do you have a tip how to achieve this with one single call?

  • 写回答

0条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥30 如何降低hdfs中datanode的JVM内存用量
      • ¥20 SQL数据查询,子查询
      • ¥15 c++字符串分割问题
      • ¥15 vue+uniapp
      • ¥15 android freedom
      • ¥15 使用自定义的类型代替内置类型可行吗
      • ¥15 关于STM32的SPI和ENDAT接口编码器通信的问题
      • ¥15 关于#pdfbox#生成的PDF文件正常,转图片中文乱码的问题,如何解决?
      • ¥15 ADS中有关DAC控件的使用问题
      • ¥15 win11如何运行geoserver