Javascript XMLHttpRequest和数据来自Array

时间:2013-02-15 09:49:03

标签: javascript arrays xmlhttprequest onreadystatechange

如果在功能run() "url = 'http://localhost/' + Math.random().toString(36).substr(2,16) + '.html';"上,则调用xmlhttp.status == 404 ... 但"url = 'http://' + sData[0] + '/' + Math.random().toString(36).substr(2,16) + '.html'; "未被称为xmlhttp.status == 404 ...

 var loop_timer = 3;
 var sData = new Array();
 function startXHR()
 {
    try { return new XMLHttpRequest(); } catch(e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP.7.0"); } catch (e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP.5.0"); } catch (e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP.4.0"); } catch (e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
    alert("Bitte aktivieren Sie ActiveX-Scripting oder aktualisieren Ihren Web-Browser!");
    return null;
 }
 function run() {
    url = 'http://' + sData[0] + '/' + Math.random().toString(36).substr(2,16) + '.html';
    var xmlhttp = startXHR();
    xmlhttp.open("GET",url,true);
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 404) {
        alert('404 called');
        }
    }
    xmlhttp.send();
 }
 $(document).ready(function () {
    $("#submit_exp").click(function(){
    var sid = $("#e_server").find('option:selected').attr('id');
    if(sid==99) {
    sData = $("#e_server").val().split(",");
    } else {
    sData[0] = $("#e_server").val();
    }
    run();
    loop = setInterval("run()", loop_timer*1000);
    });
 });
<html>
<select name="server" id="e_server">
<option value="192.168.2.100,192.168.2.101,192.168.2.102,192.168.2.103,192.168.2.104" id="99" selected="selected">all Server</option>
<option value="192.168.2.100" id="0">192.168.2.100</option>
<option value="192.168.2.101" id="1">192.168.2.101</option>
<option value="192.168.2.102" id="2">192.168.2.102</option>
<option value="192.168.2.103" id="3">192.168.2.103</option>
<option value="192.168.2.104" id="4">192.168.2.104</option>
 </select>
</html>

0 个答案:

没有答案