responseXML и ошибка в Internet Explorer - PullRequest
0 голосов
/ 09 июля 2011

У меня есть приложение AJAX, которое работает на всех платформах, но не на IE9. Я использую responseXML для извлечения XML-документа обратно в приложение, и нет ответа от IE. Спасибо за ваше время.

if (!JS_AJAX) var JS_AJAX = {};
else if (JS_AJAX && typeof(JS_AJAX) != "object")
throw new Error("JS_AJAX is not an Object type");

JS_AJAX = {
NAME: "AJAX&JavaScript",
VERSION: 1.0,

addEventHandler: function(oNode, sEvt, fnHandler, bCapture) {
    if (typeof (oNode.addEventListener) != "function")
        oNode.attachEvent("on" + sEvt, fnHandler);
    else
        oNode.addEventListener(sEvt, fnHandler, bCapture);
},

removeEventHandler: function(oNode, sEvt, fnHandler, bCapture) {
    if (typeof (oNode.removeEventListener) != "function")
        oNode.detachEvent("on" + sEvt, fnHandler);
    else
        oNode.removeEventListener(sEvt, fnHandler, bCapture);
},

getEventTarget: function(evt) {
    var e = evt || window.event;
    if (e.target) return e.target;
    else return e.srcElement;
},

stopEvent: function(evt) {
    var e = evt || window.event;
    if (e.stopPropagation) e.stopPropagation();
    else e.cancelBubble = true;
},

preventDefault: function(evt) {
    var e = evt || window.event; 
    if (e.preventDefault) e.preventDefault();
    else e.returnValue = false;
},

initAJAX: function(){
    var objxml = null;
    var ProgID = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0", "Microsoft.XMLHTTP"];            

    try{
        objxml = new XMLHttpRequest();
    }
    catch(e){                
        for (var i = 0; i < ProgID.length; i++){
            try{
                objxml = new ActiveXObject(ProgID[i]);
            }
            catch(e){                        
                continue;
            }
        }
    }

    return objxml;            
},


AJAXpost: function(url, values, callBack, flowHandler){
    var request = JS_AJAX.initAJAX();
    var outObj = {};

    outObj.result = null
    outObj.loadingFlag = false;
    outObj.errorFlag = false;

    if (!request){
        outObj.result = "Unable to start AJAX session.";
        outObj.loadingFlag = false;
        outObj.errorFlag = true;

        if (flowHandler){
            flowHandler(outObj);
        }

        return outObj;
    }

    timer = setTimeout(function(){request = null;}, 5000);

    request.onreadystatechange = function(){

        if (!request){
            outObj.result = "AJAX session has expired.";
            outObj.loadingFlag = false;
            outObj.errorFlag = true;

            if (flowHandler){
                flowHandler(outObj);
            }

            return;
        }

        if (request.readyState == 4){

            if (request.status == 200){

                clearTimeout(timer);
                outObj.result = JS_AJAX.getAJAXResponse(request);
                outObj.loadingFlag = false;
                outObj.errorFlag = false;

                if (flowHandler){
                    flowHandler(outObj);
                }

                if (callBack){
                    callBack(outObj.result);
                }
            }else{
                outObj.result = "There was a problem with the request " + request.status;
                outObj.loadingFlag = false;
                outObj.errorFlag = true;
                if (flowHandler){
                    flowHandler(outObj);
                }
            }

        }else{

            if (request.status == 200){
                outObj.result = "Loading...";
                outObj.loadingFlag = true;
                outObj.errorFlag = false;
                if (flowHandler){
                    flowHandler(outObj);
                }
            }else{
                outObj.result = "There was a problem with the request " + request.status;
                outObj.loadingFlag = false;
                outObj.errorFlag = true;
                if (flowHandler){
                    flowHandler(outObj);
                }
            }
        } // End of if (request.readyState == 4){
    } // End of request.onreadystatechange = function(){

    request.open("POST", url);
    request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    request.send(JS_AJAX.encodeData(values));

},

AJAXget: function(url, values, callBack, flowHandler){
    var request = JS_AJAX.initAJAX();
    var outObj = {};

    outObj.result = null
    outObj.loadingFlag = false;
    outObj.errorFlag = false;

    if (!request){
        outObj.result = "Unable to start AJAX session.";
        outObj.loadingFlag = false;
        outObj.errorFlag = true;

        if (flowHandler){
            flowHandler(outObj);
        }

        return outObj;
    }

    timer = setTimeout(function(){request = null;}, 5000);

    request.onreadystatechange = function(){

        if (!request){
            outObj.result = "AJAX session has expired.";
            outObj.loadingFlag = false;
            outObj.errorFlag = true;

            if (flowHandler){
                flowHandler(outObj);
            }

            return;
        }

        if (request.readyState == 4){

            if (request.status == 200){

                clearTimeout(timer);
                outObj.result = JS_AJAX.getAJAXResponse(request);
                outObj.loadingFlag = false;
                outObj.errorFlag = false;

                if (flowHandler){
                    flowHandler(outObj);
                }

                if (callBack){
                    callBack(outObj.result);
                }

            }else{
                outObj.result = "There was a problem with the request " + request.status;
                outObj.loadingFlag = false;
                outObj.errorFlag = true;
                if (flowHandler){
                    flowHandler(outObj);
                }
            }

        }else{

            if (request.status == 200){
                outObj.result = "Loading...";
                outObj.loadingFlag = true;
                outObj.errorFlag = false;
                if (flowHandler){
                    flowHandler(outObj);
                }
            }else{
                outObj.result = "There was a problem with the request " + request.status;
                outObj.loadingFlag = false;
                outObj.errorFlag = true;
                if (flowHandler){
                    flowHandler(outObj);
                }
            }
        } // End of if (request.readyState == 4){
    } // End of request.onreadystatechange = function(){

    if (values) url += "?" + JS_AJAX.encodeData(values);

    request.open("GET", url);
    request.setRequestHeader("X-Requested-With", "XMLHttpRequest");

    request.send(null);

},

getAJAXResponse: function(xhr){

    switch(xhr.getResponseHeader("Content-Type")){
        case "application/xml":
        case "text/xml":
            return xhr.responseXML;
        default:
            return xhr.responseText;
    }
},

encodeData: function(data){
    var pairs = [];
    var regexp = /%20/g;

    for(var name in data){
        var value = data[name].toString();

        var pair = encodeURIComponent(name).replace(regexp, "+") + '=' + encodeURIComponent(value).replace(regexp, "+");
        pairs.push(pair);
    }

    return pairs.join('&');
}
}

А это:

JS_AJAX.addEventHandler(window, "load", init, false);

function init(evt){

attributes = {
    id: 1       
}

JS_AJAX.AJAXget("03/colors.xml", attributes, showContent, flow);
}

function showContent(result){
myDiv = document.getElementById("nesto");

myDiv.innerHTML = result;
}

function flow(obj){
myDiv = document.getElementById("nesto");

if (obj.loadingFlag){
    p = document.createElement("P");
    text = document.createTextNode(obj.result);
    p.appendChild(text);

    myDiv.parentNode.appendChild(p);
}
}

Я нашел в этом вопросе метод для анализа XML в Internet Explorer: «Ноль» - ноль или нет объектная ошибка в IE JavaScript

if (xmlhttp.responseXML.xml)
    var xmlDoc = xmlhttp.responseXML.xml; // Got same results like with responseText!
else
    var xmlDoc = xmlhttp.responseXML;

Но я получаю тот же результат, что и responseText! Моя идея - вернуть объект ..

...