English 中文(简体)
JavaServlet 的 HTML 字符串, 被解释为 JavaServlet 中的 # 文档, 而不是字符串
原标题:HTML string from JavaServlet interpreted as #document in Javascript instead of string
  • 时间:2012-05-22 14:39:56
  •  标签:
  • jquery

我们有一个 Java Servlet 运行, 为我们提供所需的数据。 问题在于 Jquery 如何解读这些数据 。

信息通过响应作者发送。 (例如: 消息)

protected void doPost(HttpServletRequest request,
    HttpServletResponse response)
        throws ServletException {

    String body = "";
    for (Message msg : messages) {
        body += "<div class="Message" id="" + msg.getId() + "">"
            + *inner information*
            + "</div>";
    }
    response.getWriter().write(body);
}

信息是通过Ajax要求获得的

$.ajax({
    type:  POST ,
    url:  message.jsp ,
    data: { *Needed data* },
    success: function(data) {
        $( #element ).append(data);
    }
});

这有时是有效的,有时是行不通的。所以我们用铬检查控制台,让代码停止在附加行。当发送多个信息时,成功函数中的数据被解释为一个大字符串,

"<div class="message" id="153" onclick="loadFullMessage(153)"></div>
<div class="message" id="154" onclick="loadFullMessage(154)"></div>
<div class="message" id="155" onclick="loadFullMessage(155)"></div>
<div class="message" id="156" onclick="loadFullMessage(156)"></div>
"

但只发送了 1 个消息/ div 时,它被解释为 # document 对象。

#document
    <div class=​"message" id=​"174" onclick=​"loadFullMessage(174)​">​…​</div>​

电文可在铬控制台中看到,但如果附加以下错误,则会遇到未发现错误:HIERARCHY_REQUEST_ERR:DOM例外3。

如何将数据总是解释为字符串而不更改为文档对象?

最佳回答

@boblail 是对的 - 将 dataType 更改为 ajaax 将会成功。 但是, 您有时会使用第三方图书馆, 而您不会修改这些图书馆 。 在我的案例中, 它是 JQuery 界面 。

发送 Content-Type: application/xhtml+xml 导致 JQuery 从响应中创建 DOM 文档。 设置您的 Content- Type text/ html , 您很好 。

问题回答

我刚通过在 dataType 调用 ajax 调用中添加 dataType 解决了一个非常类似的问题:

$.ajax({
  type:  POST ,
  url:  message.jsp ,
  data: { *Needed data* },
  success: function(data) {
    $( #element ).append(data);
  }
});

另见这两个Stack Overform答复:





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签