IE & Ajax / XPath

2019-09-18 02:37发布

问题:

I've read countless threads and tried implementing many different suggestions, but haven't had any luck.

first:

function ajaxRequest() {
        try {
                var request = new XMLHttpRequest();
        }
        catch(e1) {
                try {
                        var request = new ActiveXObject("Msxml2.HTMLHTTP");
                }
                catch(e2) {
                        try {
                                var request = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        catch(e3) {
                                var request = false;
                        }
                }
        }
        return request;
}

It looks like IE is successfully using XMLHttpRequest. As far as I can tell, it's loading the XML fine, but Xpath is another story:

function XMLPath(doc, path) {
    try {
            return doc.evaluate(path, doc, null, XPathResult.STRING_TYPE, null).stringValue;
    } catch (e) {
            try {
                    doc.setProperty("SelectionLanguage", "XPath");
                    return doc.selectNodes(path);
            }
            catch(e2) {
                    alert(e2);
            }
    }

}

Basically, what must I change in my catch statement to make it work with IE? What's also interesting is that it never alerts an e2 error, meaning it's not actually throwing an error. Totally confused.

Thanks.

回答1:

Try return doc.selectSingleNode(path).text; for IE, that is the closest you can get for accessing the string value of the node found by your path.