1

I am using Spring MVC and Spring Security for the application. We are using spring form tags to request rest calls in the application. For a particular purpose, i am using ajax calls from the jsp. It calls the controller method successfully and does processing, but, the controller method doesn't land to a view for some reason. I am not getting any error or anything in logs.

Controller:

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

javascript function written in jsp:

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

        }
Narendra Rawat
  • 353
  • 2
  • 5
  • 17
  • I don't think it is possible to redirect from AJAX request. You can return url to which you want to redirect and then redirect from frontend – user2452483 Nov 23 '18 at 10:49
  • what do you mean? this is what spring controller does. – Narendra Rawat Nov 23 '18 at 12:08
  • You are using AJAX hence you need to redirect on the client.... What currently happens is that the 302 is handled by the browser and the result in the AJAX call is the HTML (or whatever you are redirecting) too. You should use that to re-render your page. Or instead of doing it like this, return a URL and redirect in your AJAX success function. – M. Deinum Nov 23 '18 at 12:17

1 Answers1

0

It is not possible to redirect by this way in ajax call.

Better you return one success or failure flag and in the ajax success function do below.

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

In view page use this to redirect :

success: function(response, data){
      if(data=='success'){
           window.location.replace('<redirect url>');
      }
},
Alien
  • 15,141
  • 6
  • 37
  • 57