简体   繁体   English

如何在对AJAX的响应中包含多个呈现的JSP?

[英]How to include multiple rendered JSP into response to AJAX?

I need to send ajax request to java back-end and to response (from java back-end) with two html-blocks as answer. 我需要将ajax请求发送到Java后端,并使用两个html块作为答案来响应(来自Java后端)。 I want to generate those two html-blocks using two different JSPs. 我想使用两个不同的JSP生成这两个html块。 I do this as following: 我这样做如下:

req.setAttribute(...);
...
resp.setContentType("text/html");
RequestDispatcher dispatcher = req.getRequestDispatcher("one.jsp");
dispatcher.include(req, resp);
dispatcher = req.getRequestDispatcher("two.jsp");
dispatcher.include(req, resp);

And it works. 而且有效。 But on the front-end I receive an answer like one solid html code (rendered one.jsp + rendered two.jsp). 但是在前端我会收到一个像一个坚实的html代码(呈现为one.jsp +呈现为two.jsp)的答案。 But I need to receive it as two separate html blocks to put each block to it's own . 但是我需要将其作为两个单独的html块接收,以将每个块放到自己的html块中。 What is the proper way to do this? 正确的方法是什么?

Ajax code: Ajax代码:

    function addNew() {
        var request = $.ajax({
            url: "myUrl",
            type: "post",
            dataType: "html",
            success: function(data) {
                $("#divNameOne").html(<one part of data>);
                $("#divNameTwo").html(<second part of data>);
            },
            error:function() {
                alert("fail");
            }
        });
    }

In your success function , 在您的成功职能中,

var reponseHtml = $(data); // or you can use $($.parseHtml(data));
$("#divNameOne").html(responseHtml.find("#div1").html());
$("#divNameTwo").html(responseHtml.find("#div2").html());

It might work. 它可能会起作用。

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

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