javascript - application/xml 与 text/xml 内容类型

标签 javascript xml ajax http get

我正在尝试从 servlet 获取 XML 响应。 servlet 返回内容类型“application/xml”。使用 XmlHttpRequest,我可以获得 responseText,但不是 responseXml。我想知道这是否与内容类型或请求类型有关(我正在执行 GET)...?

非常感谢!

我已经精简了所有文件。我认为我设置正确。这是我拥有的:

-------- HTML ------

<html>
<head>
<title></title>
<script src="js/jboard_simple.js" type="text/javascript"> </script>
</head>
<body>

    <div id="myDiv">
        <h2>No results yet....</h2>
    </div>

    <form name="searchForm" id="searchForm_id">
        <input type="text" name="searchString" id="searchString_id" />
        <button type="button" onclick="loadXMLDoc()">Perform Search</button>
    </form>

</body>
</html>

-------- JavaScript ------------

function loadXMLDoc() {
    document.getElementById("myDiv").innerHTML = "searching...";

    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {

        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            // Do something here...
            alert(xmlhttp.responseText);
            alert(xmlhttp.responseXml);

            processSearchServletResponse(xmlhttp.responseText);
        }
    }

    // Find teh search string
    var searchString_el = window.document.getElementById('searchString_id');
    var searchString = searchString_el.value;
    alert('searchString: ' + searchString);

    var searchUrl = "/SimpleServlet?searchString=" + searchString;

    xmlhttp.open("GET", searchUrl, true);
    xmlhttp.send();
}

function processSearchServletResponse(xmlTxt) {
    document.getElementById("myDiv").innerHTML = xmlTxt;    
}

-------- 小服务程序 --------

import java.io.BufferedOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;

public class SimpleServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private static Logger logger = LogManager.getRootLogger();

    public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        BufferedOutputStream bs = null;
        String simpleResponse = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>hi</root>";


        try {
            res.setContentType("text/xml");
            res.setCharacterEncoding("UTF-8");

            bs = new BufferedOutputStream(res.getOutputStream());
            bs.write(simpleResponse.getBytes());

        } catch (Exception ex) {
            logger.error("JboardSearchServlet.service(): error = ", ex);
        } finally {

            bs.flush();
            bs.close();
        }
    }
}

最佳答案

亲爱的!我刚刚弄清楚了我的整个问题(在别人看过之后)。

我使用的是“responseXml”而不是“responseXML”。该死的大写字母 =)

关于javascript - application/xml 与 text/xml 内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6657787/

相关文章:

javascript - 使用 jquery 以 (mm/dd/yyyy) 格式获取两个日期之间的月差

javascript - Sequelize.define 无法将可选对象合并到模型构造函数中

javascript - Meteor 应用程序 - 主干路由器未被调用

xml - 获取不同层中的所有 <PRO>-标签

javascript - 对于 IE 中的 ajax 内容,onload 触发过早

javascript - 如何在没有客户端脚本的情况下完全折叠和更改网站内容?

javascript - Ajax post 带有参数数组,然后在 PHP 中引用

javascript - 在 React 中操作模型实例的正确方法是什么?

c# - 如何使用 Linq To XML 获取元素值

java - imdb 仅获取电影摘要 java (xml)