1

我有 2 个 .jsp 页面。

The 1. one contains just a xml structure for applicants:

<% response.setContentType("text/xml") ; %>

    <applicant>
     <citizenship>GERMANY</citizenship>
     <residence>Inc.</residence>
     <street>9500 Gilman Drive</street>
     <city>La Jolla</city>
     <state>USA</state>
     <countryTelCode>Vandelay Industries</countryTelCode>
     <zipCode>Inc.</zipCode>
     <areaCode>9500 Gilman Drive</areaCode>
     <telNumber>La Jolla</telNumber>
     <major>USA</major>
     <awarded>Vandelay Industries</awardeds>
     <gpa>Inc.</gpa>
     <specialization>9500 Gilman Drive</specialization>
    </applicant>

第二个尝试从标签中检索 GERMANY 并将其打印在“...”字段中:

<span id="Citizenship">...</span>

在调用 showCustomer() 后使用此代码:

<script type="text/javascript">
    function showCustomer() {
        var xmlHttp;

        xmlHttp = new XMLHttpRequest();
        if (xmlHttp == null) {
            alert("Your browser does not support AJAX!");
            return;
        }
        var url = "getApplicant_xml.jsp";

        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {
                var xmlDoc = xmlHttp.responseXML.documentElement;
                document.getElementById("Citizenship").innerHTML = xmlDoc.getElementsByTagName("citizenship")[0].childNodes[0].nodeValue;

            }

        }
        xmlHttp.open("GET", url, true);

        xmlHttp.send(null);
    }
    }
</script>

不幸的是它没有打印任何东西......如果有人发现我的错误,我将非常感激。

谢谢

4

1 回答 1

1

您的问题是 xmlHttp.responseXML 为空。您需要从 xmlHttp.responseText 解析一个新的 DOM 对象。我修复了代码。

<script type="text/javascript">
function showCustomer() {
    var xmlHttp;
    xmlHttp = new XMLHttpRequest();
    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    var url = "getApplicant_xml.jsp";
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            var xmlDoc = xmlHttp.responseText;
            xmldom = (new DOMParser()).parseFromString(xmlDoc, 'text/xml');
            text = xmldom.getElementsByTagName("citizenship")[0];
            document.getElementById("Citizenship").innerHTML = text.childNodes[0].nodeValue;
        }
    };
    xmlHttp.open("GET", url, true);

    xmlHttp.send(null);
};
</script>
于 2012-05-24T07:45:38.643 回答