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.
- On SUCCESS, it is not redirecting, it stays on same URL & gives a 415
- 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.
What am I doing wrong here?