简体   繁体   中英

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. I want to generate those two html-blocks using two different JSPs. 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). But I need to receive it as two separate html blocks to put each block to it's own . What is the proper way to do this?

Ajax code:

    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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