4

I am using Jquery to send an AJAX POST request. On success I need to redirect the user to a different page, on failure I want to stay on the page with the same data.

I am having 2 issues.

  1. On SUCCESS, it is not redirecting, it stays on same URL & gives a 415
  2. On FAILURE, I get a 415 instead of the page getting rendered.

Here is my code:

 var jsonResponse = [];
 //jsonResponse is being constructed from the form data, on submit.

 var req = {
            "name" : "${name}",
            "id" : "${id}",
            "data" : jsonResponse
    };


 $.ajax({
        url: "url",
        type: "POST",
        dataType: 'json',
        contentType:'application/json',
        async: false,
        data: JSON.stringify(req),
        success: function(result) {

            url = window.location.href;
            url = url.replace("processData", "getData");
            alert(url); //prints localhost:8000/getData
            location.href = url; //stays on localhost:8000/processData
        },
        error: function() {
            alert("--- Failure ---");
            // should stay on same page with the previous data, but
            //returns a 415.
        }
    });

Controller for url1

 @RequestMapping(value = "processData", method = RequestMethod.POST)
public String post( Model model,
                    HttpServletRequest httpServletRequest, 
                    @RequestBody FormModel model) {

}

Controller for url2:

@RequestMapping(value = "getData", method = RequestMethod.GET)
public String get(Model model, HttpServletRequest httpServletRequest)
{
}

I am attaching snapshot of the Error Message I get. enter image description here

What am I doing wrong here?

AgentX
  • 1,402
  • 3
  • 23
  • 38
  • would you try removing contentType:'application/json' from ajax paramter if it works – arun bahal Oct 05 '15 at 06:03
  • do you get this error as a response from ur AJAX call ? or when your SUCCESS/ERROR function is executed ? – Ronak Patel Oct 05 '15 at 06:08
  • @Patel when the success/error callback is executed, it prints the alerts() and then renders a 415 exception. – AgentX Oct 05 '15 at 06:09
  • @arunbahal It doesn't work. After removing it, control does not even reach the controller. – AgentX Oct 05 '15 at 06:10
  • You have not shared full URLs. It would be great if you provide full URLs as well as what data you are passing – Ronak Patel Oct 05 '15 at 06:10
  • @Patel updated the question with urls and data. My controller is able to process the data. The issue occurs when I return the control back to the jsp(ajax callback) – AgentX Oct 05 '15 at 06:16
  • Have you added jackson classes in classpath? – Tobías Oct 05 '15 at 08:49
  • @Tobías yes, I found the issue, It was that the form was also submitting the data & the page was getting refreshed. – AgentX Oct 05 '15 at 08:58

1 Answers1

0

The issue was with the form. I had a Submit button which was also submitting the data after AJAX call ended. So I had to remove that functionality.

Initially:

<input type="Submit" value="Submit" />

Now:

<input type="button" value="Submit" />

Better approach is to override the default Submit functionality.

 $('form').on('submit', function(e) {
    e.preventDefault();

Here the answer to a question I asked : Form Processing via AJAX - Avoiding both GET & POST Requests from getting generated

Community
  • 1
  • 1
AgentX
  • 1,402
  • 3
  • 23
  • 38