【问题标题】:How to redirect page after ajax complete?ajax完成后如何重定向页面?
【发布时间】:2016-08-26 12:42:46
【问题描述】:

我的 ajax 执行成功,因为它从数据库中删除了行:

    @Controller
    ...
    @ResponseBody
    @RequestMapping(value = "/ajaxDeleteUser", method = RequestMethod.POST)
    public ModelAndView ajaxDelUser(HttpServletRequest request) {
        int userId = Integer.parseInt(request.getParameter("id"));
        userDao.delete(userId);
        return new ModelAndView("redirect:/");
    }
    ...

在视图中:

<c:url value="/" var="home" scope="request" />
$.ajax({
        data: {"id":data},
        type: "POST",
        url:  "${home}"+"ajaxDeleteUser",
        async: false
});

问题是ajax 完成后,我看到了列表中的行!那么ajax完成后如何重定向到列表实际页面呢?

更新:

这里是 servlet-context.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.ambre.hib" />

</beans:beans>

这里是 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

【问题讨论】:

    标签: spring spring-mvc spring-4


    【解决方案1】:

    考虑使用“成功”事件:http://api.jquery.com/Ajax_Events/

    $.ajax({
            data: {"id":data},
            type: "POST",
            url:  "${home}"+"ajaxDeleteUser",
            async: false,
            success: function(result) {
                location.href = "your_location"
            }
    });
    

    【讨论】:

    • @pheromix 尝试“错误”事件而不是“成功”,也许您的请求没有成功
    • “不起作用”是什么意思?你检查了浏览器的控制台,日志里有什么吗?
    • 好的,我找到了问题的根源:我将启动ajax的链接的href属性值设置为'#'。
    • 那是因为需要将返回类型改为String,并在ajax中返回要重定向到的页面的名称
    【解决方案2】:

    这里有两个选项。首先,您可以在ajax完成后使用ajax成功函数在前端重定向,如下所示:

    success: function(result) {
                location.href = "your_location"
            }
    

    或者最好你可以在后端使用 spring mvc 来完成这项工作。为此,请删除您不需要的 @Responsebody 因为您不需要它(您没有返回任何内容),并将返回类型更改为如下所示的字符串。

    @RequestMapping(value = "/ajaxDeleteUser", method = RequestMethod.POST)
    public String ajaxDelUser(HttpServletRequest request) {
        //your stuff goes here
        ....
        //important line
        return "redirect:/index.html";
    }
    

    我在这里使用 index.html,但将名称更改为您喜欢的页面。只要您正确配置了 Spring 视图解析器,它就可以自行工作。如果您在视图解析器中添加了后缀(例如 .jsp),那么您应该只返回不带后缀的页面名称,spring 会自动找出页面,如下所示

    return "redirect:/index";  //equals index.jsp
    

    【讨论】:

    • 控制器中返回字符串的方法不起作用!
    • 发布你的spring webconfigurer,你有viewResolver吗?你得到什么错误?
    • 它是否击中了控制器?如果您在控制器中放置一个制动点,它会去那里吗?那个“${home}”+“ajaxDeleteUser”是什么? ajax 请求中的 ${home} 是什么
    【解决方案3】:

    你可以使用这个。这将在响应到达后重定向。

    $.ajax({
        data:{"id":data},
        type: "POST",  
        url:  "${home}"+"ajaxDeleteUser",  
        async: false,  
        success: function(result) {  
            var link = "http://www.sample.com";    
            $(location).attr('href',link);  
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 2015-07-14
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      • 1970-01-01
      • 2012-05-20
      相关资源
      最近更新 更多