gpt4 book ai didi

ajax - 在Ajax响应中获取整个页面: Spring MVC, Ajax

转载 作者:行者123 更新时间:2023-12-01 02:30:54 24 4
gpt4 key购买 nike

我正在处理 ajax 请求/响应来更新表,

但是当 Ajax 调用 Controller 时,我得到了整个页面的响应。但我想要的只是需要用 c:forEach 表映射的精确表数据。谢谢。

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);
}
}

更新{1}:

我尝试更改 Controller ,但它开始出现错误:[object XMLHttpRequest]

 @RequestMapping("/noticesAjaxRequest")
public @ResponseBody List<Notice> noticesAjaxRequest(@ModelAttribute("NoticeForm") NoticeForm noticeForm, ModelMap model) {

noticeBO.prepareNoticeList(noticeForm,model);

return noticeForm.getNoticeList();
}

错误描述:

HTTP 状态 406 - 此请求标识的资源只能生成具有根据请求“接受” header Not Acceptable 特征的响应。

响应 header

Content-Length  1067
Content-Type text/html;charset=utf-8
Date Thu, 11 Jul 2013 12:48:19 GMT
Server Apache-Coyote/1.1

请求 header

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

最佳答案

感谢各位 friend 的帮助。最好、最简单的方法是创建单独的 jsp View ,并在从 Controller 返回/响应时包含该 View 。

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);
}

关于ajax - 在Ajax响应中获取整个页面: Spring MVC, Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17593187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com