In Ajax Response getting whole page : Spring MVC, Ajax
ajax, jquery, spring-mvc
Solution
Thank you friends for your help. Best and Easy one is to create the separate jsp view and include that while returning/responding from controller.
jQuery(document).ready(function(){
function doAjaxPost() {
// get the form values
var contextPath ='<jsp:expression>contextPath</jsp:expression>';
$.ajax({
type: "GET",
url: contextPath+"/noticesAjaxRequest",
success: function(data){
// we have the response
$('#info').html(data);
/* $('#info').refresh(); */
},
error: function(e){
alert('Error: ' + e);
}
});
}
setInterval(doAjaxPost,10*1000);
});
Controller:
@RequestMapping(value="/noticesAjaxRequest")
public ModelAndView noticesAjaxRequest(@ModelAttribute("NoticeForm") NoticeForm noticeForm, ModelMap model) {
noticeBO.prepareNoticeList(noticeForm,model);
return new ModelAndView("/noticesList","noticeForm",noticeForm);
}
Problem
I am working on a ajax request/response to update a table, But when Ajax put a call to controller I am getting whole page in response. But what i want is only precise table data which i need to map with my c:forEach table. Thanks. Jsp View: ``` <script type="text/javascript"> jQuery(document).ready(function(){ function doAjaxPost() { // get the form values var contextPath ='<jsp:expression>contextPath</jsp:expression>'; $.ajax({ type: "GET", url: contextPath+"/noticesAjaxRequest", dataType: "json", contentType: 'application/json', success: function(data){ // we have the response $('#info').empty().html(data); }, }); } setInterval(doAjaxPost,10*1000); }); </script> <div id="info"> <c:forEach items="${noticeForm.noticeList}" var="notice"> <c:out value="${notice.coreValue} "/> <c:out value="${notice.description} "/> <br/> </c:forEach> </div> ``` Controller: ``` @Controller public class DashboardController { private NoticeBO noticeBO; /*@RequestMapping("/dashboardTest") public String printWelcome(ModelMap model) { List<Employee> employeeList=dashboardDAO.getAllEmployee(); for(Employee employee:employeeList) model.addAttribute("msg", model.get("msg")+"<br/> Spring 3 MVC Hello World"+employee.getCustomerId()); return "DashboardTest"; }*/ public NoticeBO getNoticeBO() { return noticeBO; } public void setNoticeBO(NoticeBO noticeBO) { this.noticeBO = noticeBO; } @RequestMapping("/dashboard") public String dashboard(ModelMap model) { return "Dashboard"; } @RequestMapping("/notices") public ModelAndView notices(@ModelAttribute("NoticeForm") NoticeForm noticeForm, ModelMap model) { noticeBO.prepareNoticeList(noticeForm,model); return new ModelAndView("notices","noticeForm",noticeForm); } @RequestMapping("/noticesAjaxRequest") public ModelAndView noticesAjaxRequest(@ModelAttribute("NoticeForm") NoticeForm noticeForm, ModelMap model) { noticeBO.prepareNoticeList(noticeForm,model); return new ModelAndView("notices", "noticeForm", noticeForm); } } ``` Update{1}: I tried a change in controller and it starts giving me Error: [object XMLHttpRequest] ``` @RequestMapping("/noticesAjaxRequest") public @ResponseBody List<Notice> noticesAjaxRequest(@ModelAttribute("NoticeForm") NoticeForm noticeForm, ModelMap model) { noticeBO.prepareNoticeList(noticeForm,model); return noticeForm.getNoticeList(); } ``` Error Description: HTTP Status 406-The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers. Response Headers ``` Content-Length 1067 Content-Type text/html;charset=utf-8 Date Thu, 11 Jul 2013 12:48:19 GMT Server Apache-Coyote/1.1 ``` Request Headers ``` Accept application/json, text/javascript, */* Accept-Encoding gzip, deflate Accept-Language en-US,en;q=0.5 Cookie JSESSIONID=D54D66F6B7FE05C2B6FB684BF19387F1 Host localhost:8080 Referer http://localhost:8080/vServFinance/notices User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0 X-Requested-With XMLHttpRequest ```