Issue
I have a Spring Boot @Controller that redirect a page after ajax call. The code doesn't show error, but the new page not appear. Bellow I shared the pice of controller's code and the js function. Does anyone know what is happen?
Controller:
@RequestMapping(value = "/openNewPage", method = RequestMethod.GET)
public ModelAndView openNewPage(@RequestParam String id, Model model) {
ModelAndView mav = new ModelAndView();
mav.setViewName("/newPage");
return mav;
}
JS:
function open() {
var table = $('#dataList').DataTable();
$('#dataList tbody').on('click', 'tr', function() {
ligneSel = table.row(this).data();
var id = ligneSel[0];
$.ajax({
url : './openNewPage',
type: 'get',
dataType : 'html',
data : {
id: id
},
success : function(data) {
if (data != null) {
$("body").html(data);
}
}
});
});
}
Solution
My problem is the ajax call to spring boot controller does not redirecting a new view. The answer is that we need to call the controller twice.
This post will explain better the solution : Ajax call to spring boot controller to redirecting a view .
Thanks for all that helped me in this issue.
The exemple below show the solution:
1) First controller:
@RequestMapping(value = "/openNewPage", method = RequestMethod.GET)
public ModelAndView openNewPageController1(@RequestParam String id, Model model) {
ModelAndView mav = new ModelAndView("/newPage");
return mav;
}
2) Second controller:
@RequestMapping(value = "/openNewPage", method = RequestMethod.GET)
public ModelAndView openNewPageController2(@RequestParam String id, Model model) {
ModelAndView mav = new ModelAndView("/newPage");
return mav;
}
3) The javaScript:
function open() {
var table = $('#dataList').DataTable();
$('#dataList tbody').on('click', 'tr', function() {
ligneSel = table.row(this).data();
var id = ligneSel[0];
$.ajax({
url : './openNewPage',
type: 'get',
dataType : 'html',
data : {
id: id
},
success : function(data) {
window.location.href = "/openNewPage?id="+id;
}
});
});
}
Answered By - Luciana Oliveira