簡體   English   中英

如何在 Ajax 成功方法中將 Spring ModelAndView 響應作為完整頁面打開

[英]How to open Spring ModelAndView response in Ajax success method as a complete page

我無法使用 Ajax 成功方法打開 Spring 控制器方法給出的ModelAndView響應。

彈簧控制器方法:

    @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;
    }

阿賈克斯代碼:

$("#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 (我想打開的頁面):

<%@ 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>

我不想在彈出窗口中打開此頁面,而是希望在同一頁面上打開它以替換舊頁面。

在同一頁面上,您應該指定應該注入返回的 html 文本的目標。 例如

<div id="result"/>

然后在success處理程序加載內容

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM