Ajax request doesn't redirect to view in Spring MVC
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);
}
});
}
javascript java spring spring-mvc model-view-controller
add a comment |
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);
}
});
}
javascript java spring spring-mvc model-view-controller
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
add a comment |
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);
}
});
}
javascript java spring spring-mvc model-view-controller
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);
}
});
}
javascript java spring spring-mvc model-view-controller
javascript java spring spring-mvc model-view-controller
asked Nov 23 '18 at 10:46
Narendra RawatNarendra Rawat
102213
102213
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
add a comment |
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
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
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
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
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
add a comment |
1 Answer
1
active
oldest
votes
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>');
}
},
did it work...?
– Alien
Nov 29 '18 at 12:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445195%2fajax-request-doesnt-redirect-to-view-in-spring-mvc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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>');
}
},
did it work...?
– Alien
Nov 29 '18 at 12:58
add a comment |
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>');
}
},
did it work...?
– Alien
Nov 29 '18 at 12:58
add a comment |
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>');
}
},
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>');
}
},
answered Nov 23 '18 at 12:18
AlienAlien
4,85331026
4,85331026
did it work...?
– Alien
Nov 29 '18 at 12:58
add a comment |
did it work...?
– Alien
Nov 29 '18 at 12:58
did it work...?
– Alien
Nov 29 '18 at 12:58
did it work...?
– Alien
Nov 29 '18 at 12:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445195%2fajax-request-doesnt-redirect-to-view-in-spring-mvc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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