简体   繁体   English

AJAX检索xml响应

[英]AJAX retrieving xml response

I have 2 .jsp pages. 我有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>

The second one tries to retrieve GERMANY from the tag and print it in the "..." field of: 第二种尝试从标签中检索德国并将其打印在以下位置的“ ...”字段中:

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

using this code after calling showCustomer(): 在调用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>

Unfortunately it does not print anything.... I would be really thankful if someone finds my mistake. 不幸的是,它无法打印任何内容。...如果有人发现我的错误,我将非常感激。

Thank you 谢谢

Your problem is that xmlHttp.responseXML is null. 您的问题是xmlHttp.responseXML为空。 You need to parse a new DOM object from xmlHttp.responseText. 您需要从xmlHttp.responseText解析一个新的DOM对象。 I fixed the code. 我修复了代码。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM