JPG
JPG

Reputation: 1253

How to open Spring ModelAndView response in Ajax success method as a complete page?

I am unable to open ModelAndView response given by Spring controller method using Ajax success method.

Spring Controller method:

    @RequestMapping (value="setupDbEdit.htm", method=RequestMethod.GET)

    ModelAndView setupABC(@RequestParam String db_id){
        System.out.println("**hello world**"+db_id);
        ModelAndView modelAndView=new ModelAndView("editDB");
        request.setAttribute("dbID", db_id);
        return modelAndView;
    }

    @RequestMapping (value="editDB.jsp", method=RequestMethod.GET) 
    ModelAndView addCustomerSetupABC(){
        ModelAndView modelAndView= new ModelAndView("editDB");
        return modelAndView;
    }

Ajax code:

$("#editDb").click(function(e){
             $.ajax({
                type: 'GET', 
                'url': 'http://localhost:8080/Test_ReportingUI/setupDbEdit.htm',
                data: {db_id: dbID},
                dataType: 'html',
                success: function(response){ 
                    alert(response);
                },
                timeout: 10000,
                error: function(xhr, status, err){ 
                }
            }); 
         });

editDB.jsp (The page that I want to open):

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<script>

alert("hellow");
</script>
<%
String strDBID=(String) request.getAttribute("dbID");
out.println(strDBID+"<br><br>");
%>
This is edit screen for DB details...
</body>
</html>

I don't want to open this page in pop up window, rather I want to get it open on same page by replacing the old one.

Upvotes: 0

Views: 3815

Answers (1)

Roman C
Roman C

Reputation: 1

On the same page you should specify the target where the returned html text should be injected. For example

<div id="result"/>

then on success handler load the content

success: function(response){ 
    $("#result").html(response);
},

Upvotes: 3

Related Questions