How to send a JSP call from AJAX?

This is a servlet code, here the 1st one I want to send a return message (if message! = Null) to Ajax for a warning, and the second one if the message == null, I want to call another jsp to pass a list to this jsp.

    if(message!=null){
                response.setContentType("text/plain");
                response.getWriter().write(message);
            }else{
                JSONObject jobj = new JSONObject();
                String urlToRedirect = "SearchEmployee.jsp";
                jobj.put("url",urlToRedirect );
                response.getWriter().write(jobj.toString());
                }

Here I can not understand how to call this jsp url in another part

    $.ajax({
        type:"POST",
        url:'../SearchEmployeeServlet',

        data:$('#DisplayEmployeeListForm').serialize(),//$('form').serialize();
        success:function(msg){
            if(msg!=""){
                alert(msg);
                window.location.reload(true);
            }else{
                ....here i want to call 2nd jsp

            }

        }
    });
+4
source share
2 answers

You can simply redirect the request server so as not to require the client to execute the second request:

request.getRequestDispatcher("SearchEmployee.jsp").forward(request, response);

Or, alternatively, you can send an HTTP redirect response, so the client should handle the redirect to the second request automatically:

response.sendRedirect(response.encodeRedirectUrl("SearchEmployee.jsp"));

, , JavaScript, , SearchEmployee.jsp. , XML text/xml, jQuery XML DOM success:

success:function(msg) {
    if (msg instanceof XMLDocument) {
        // handle list
    } else {
        alert(msg);
        window.location.reload(true);
    }
}

HTML, , , HTML. , HTML <ul>:

success:function(msg) {
    if (msg.startsWith("<ul>")) { // note you may need to do msg = $.trim(msg) first
        // handle list
    } else {
        alert(msg);
        window.location.reload(true);
    }
}

, , , - .

if(msg!="") success , true, ( ).

, , content-type , , JSON :

response.setContentType("application/json");
response.getWriter().write(jobj.toString());

, jQuery , JSON success. , , jQuery, :

success:function(msg){
    if (typeof msg === "object") {
        // you got the JSON response
        $.ajax({
            url: msg.url,
            // etc...
        });
    } else {
        // you got the plain text response
        alert(msg);
        window.location.reload(true);
    }
}
+1

ajax , .

$.ajax({url: "jsp url", success: function(result){

}});

0

Source: https://habr.com/ru/post/1614240/


All Articles