Javascript / ajax代码 - 适用于chrome和firefox但不适用于IE10

时间:2013-07-26 21:13:04

标签: javascript ajax internet-explorer

我要做的是根据用户在之前的选择框中选择的内容来限制一个选择框的选项。它在Chrome和Firefox中完美运行,但在IE 10中,唯一显示的是文本“Not Found”。我不确定,但我的猜测是request.status出了问题。然而,它是什么,我不知道。

function prepForms() {
for (var i = 0; i<document.forms.length; i++) {
    var thisform = document.forms[i];
    var departCity = document.getElementById("departcity");
    departCity.onchange = function() {
        var new_content = document.getElementById("ajaxArrive");
        if (submitFormWithAjax(thisform, new_content)) return false;
        return true;
    }
}
}

function getHTTPObject() {
if (typeof XMLHttpRequest == "undefined")
    XMLHttpRequest = function() {
        try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
            catch (e) {}
        try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
            catch (e) {}
        try { return new ActiveXObject("Msxml2.XMLHTTP"); }
            catch (e) {}
        return false;
    }
return new XMLHttpRequest();
}

function submitFormWithAjax(whichform, thetarget) {
var request = getHTTPObject();
if (!request) {return false;}
var dataParts = [];
var element;
for (var i = 0; i<whichform.elements.length; i++) {
    element = whichform.elements[i];
    dataParts[i] = element.name + "=" + encodeURIComponent(element.value);
}
var data = dataParts.join("&");
request.open("POST", "flightlocationfilter.asp#ajaxArrive", true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.onreadystatechange = function() {
    if (request.readyState == 4) {
        if (request.status == 200 || request.status == 0) {
            var matches = request.responseText.match(/<div id="ajaxArrive">([\s\S]+)<\/div>/);
            if (matches.length > 0) {
                thetarget.innerHTML = matches[1];
            } else {
                thetarget.innerHTML = "<p>--Error--</p>";
            }
        } else {
            thetarget.innerHTML = "<p>" + request.statusText + "</p>";
        }
    }
};
request.send(data);
return true;
};

编辑:在使用IE Developer Tools之后,看起来request.readyState的速度不会超过1到4。

0 个答案:

没有答案