这是我的第一篇文章。我很抱歉我的英语...
我在使用 postJSON 和使用 ModelAndView 返回部分 View 时遇到问题。
我的 Controller :
@RequestMapping(method=RequestMethod.POST, value = "/addUrl.html")
public @ResponseBody ModelAndView addSubMenu(@RequestBody Menu menu) {
ModelAndView mav = new ModelAndView(PathConfig.MENU_DIR + "show_url");
int id = menuService.saveOrUpdateMenu(1, menu.getTitle(), menu.getUrl(), 4, "pl");
mav.addObject("submenu", menuService.get(id));
return mav;
}
我的ajax代码:
$("#menuUrl").submit(function(){
var menu = $(this).serializeObject();
$.ajax({
type: "POST",
url: config.resourcePath+"/addUrl.html",
data: JSON.stringify(menu),
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function(response){
$( "#site" ).append(response);
},
error: function(e){
alert("Server did not response.");
}
});
});
但是...我有错误:服务器没有响应...
如何使用 json 渲染部分 View ?
谢谢。
最佳答案
这里可能有一些错误:
通过指定 application/json
的 Content-type
header ,您期望返回 json 的响应,但是看看您处理响应的方式,它看起来像你想要的是一个 html
如果您要查找的响应是 html,则从请求中删除 Content-Type header ,从 ModelAndView
响应中删除 @ResponseBody
注释,而不是只需返回一个指向普通 jsp 的 View ,该 jsp 可以创建您期望的 html 响应..
@RequestMapping(method=RequestMethod.POST, value = "/addUrl.html")
public ModelAndView addSubMenu(@RequestBody Menu menu) {
...
}
关于java - Spring 3.1,JSON响应ModelAndView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13810004/