【问题标题】:Cannot display response of HTML servlet in Ajax and qTip2无法在 Ajax 和 qTip2 中显示 HTML servlet 的响应
【发布时间】:2015-06-21 15:32:01
【问题描述】:

我正在尝试在 jsp 内的 qTip2 工具提示中显示从 servlet 响应接收到的文本(或 html)。我几乎所有东西都在工作,并且已经用 Firebug 验证了正在调用 servlet 并且正在返回文本,但是当我尝试在我的 ajax 调用中使用“html”(或数据)变量时,我收到一个错误: HierarchyRequestError:无法在层次结构中的指定点插入节点。

我尝试在 JavaScript 警报中显示 html,结果如下:[object XMLDocument]。

这是事件的顺序:

1.用户点击一段HTML文本,其中定义了一个指向servlet并传递参数的链接

2.ajax调用servlet进行一些处理并返回文本或html

3.文本显示为带有qTip2的工具提示

如何正确处理来自 servlet 的响应并处理从它接收到的文本?

Ajax 调用:

$(".ajax_link").click(function(e) {
    e.preventDefault();
    var $this = $(this);
    var link = $(this).attr('href'); //Gets link url

    $.ajax({
        type: "GET",
        url: link,
        cache: false,

    }).done(function(html) {

        $this.qtip({
            content: {
                text: html //<--this causes error above
              //text: "<table><tr><th>Team</th></tr></table>"  <--this works fine
            }
        });

        $this.qtip('toggle', true);

    });
});

Servlet 代码:

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

    System.out.println("inside doGet");

    String var1 = "<table><tr><th>Team</th></tr></table>";

    //var1 = request.getParameter("var1");

    response.setContentType("text/html");
    response.setCharacterEncoding("UTF-8");
    PrintWriter out = response.getWriter();
    out.print(var1);
    out.flush();
    out.close();
}

【问题讨论】:

    标签: java javascript jquery ajax servlets


    【解决方案1】:

    在 ajax 请求中将 dataType 作为 html 传递 即

    $.ajax({
            type: "GET",
            url: link,
            cache: false,
            dataType : "html"
    
        }
    

    【讨论】:

    • 谢谢,成功了!我之前曾尝试过,但可能我的其他设置组合错误。
    猜你喜欢
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 2016-03-25
    • 2012-11-02
    • 2017-02-09
    • 2017-12-19
    • 2021-03-17
    • 2018-05-31
    相关资源
    最近更新 更多