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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)