【问题标题】:Ajax request doesn't redirect to view in Spring MVCAjax 请求不会重定向到 Spring MVC 中的视图
【发布时间】:2019-04-25 23:28:22
【问题描述】:

我正在为应用程序使用 Spring MVC 和 Spring Security。我们正在使用 spring 表单标签来请求应用程序中的休息调用。出于特定目的,我使用来自 jsp 的 ajax 调用。它成功调用控制器方法并进行处理,但是控制器方法由于某种原因没有登陆视图。我没有收到任何错误或日志中的任何内容。

控制器:

@RequestMapping(value="/createRunOne/saveRun", method = RequestMethod.POST,  consumes = MediaType.APPLICATION_JSON_VALUE)
public String saveRun(@RequestBody Run run, Model model){

    try{
        this.runService.saveRun(run);

    } catch (Exception ex){
            model.addAttribute(ERROR, ex.getMessage());
            ex.printStackTrace();
    }
    return "redirect:/run/list";
}

jsp编写的javascript函数:

function saveRun() {

                var run = {
                    runName: $('#name').val(),
                    description: $('#description').val(),
                    justification: $('#justification').val(),
                    scheduledTime: new Date($('#scheduledTime').val()),
                    fromReceiptDate: new Date($('#fromdatepicker').val()),
                    toReceiptDate: new Date($('#todatepicker').val()),
                    sourceKeyString: $('#segmentSelect').val().toString(),
                    selectedBlocks: $('#blockList').val().toString(),
                    compareFilter: $('#filtercase').val(),
                    productsSelected: selectedList.toString()
                };

                headers['Content-Type'] = 'application/json';
                headers['dataType'] = 'json'

            $.ajax({
                type: "POST",
                url: "createRunOne/saveRun",
                data: JSON.stringify(run),
                headers: headers,
                responseType: "application/json",

                success: function(response, data){

                    if(data=='success'){

                        }
                },
                error: function (xhr,  textStatus, errorThrown) {
                    alert(xhr.status);
                }
            });

        }

【问题讨论】:

  • 我认为不可能从 AJAX 请求重定向。您可以返回要重定向的网址,然后从前端重定向
  • 什么意思?这就是弹簧控制器的作用。
  • 您正在使用 AJAX,因此您需要在客户端上重定向.... 当前发生的是 302 由浏览器处理,而 AJAX 调用中的结果是 HTML(或任何您正在重定向)。您应该使用它来重新呈现您的页面。或者不这样做,而是返回一个 URL 并在您的 AJAX 成功函数中重定向。

标签: javascript java spring spring-mvc model-view-controller


【解决方案1】:

在ajax调用中无法通过这种方式重定向。

最好返回一个成功或失败标志,并在 ajax 成功函数中执行以下操作。

@RequestMapping(value="/createRunOne/saveRun", method = RequestMethod.POST,  consumes = MediaType.APPLICATION_JSON_VALUE)
public String saveRun(@RequestBody Run run, Model model){

    try{
        this.runService.saveRun(run);

    } catch (Exception ex){
            model.addAttribute(ERROR, ex.getMessage());
            ex.printStackTrace();
    }
    return "success";
}

在视图页面中使用this 重定向:

success: function(response, data){
      if(data=='success'){
           window.location.replace('<redirect url>');
      }
},

【讨论】:

    猜你喜欢
    • 2018-07-10
    • 2018-07-09
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    相关资源
    最近更新 更多