我使用jquery发出ajax请求,这会调用以下spring控制器:
@RequestMapping(value = "/dialogController", method = RequestMethod.POST)
public String dialogController(Model model, @RequestBody MyClass myclass) {
myClass.setTitle("SUCCESS");
model.addAttribute("myClass",myClass);
return "dialogContent"; //this resolves to dialogContent.jsp
}
但是我收到以下错误:
org.springframework.web.HttpRequestMethodNotSupportedException:
Request method 'POST' not supported
如果需要,这里是我使用jQuery进行的ajax调用:
jq.postJSON("/dialogController", myClass, function(data) {
myDialog.html(data);
myDialog.dialog('open');
//dialog settings previously assigned,
//but the success callback function is not reached anyway
});
修改的 如果我使用的话,我会得到同样的错误:
jq.ajax({
type: 'POST',
url: "/dialogController",
data:myClass,
success: function(data) {
previewDialog.html(data);
previewDialog.dialog('open');
});
答案 0 :(得分:2)
对于家中的观众......我发现问题是由于控制器中定义的方法签名与ajax调用不匹配。我从控制器方法中删除了Model model
参数。然后我意识到我还必须返回一个新的模型和视图;这是工作代码:
var myJSON = {"title":"help"};
myJSON = JSON.stringify(myJSON);
<c:url var="postAndView" value="/PostJSONMAV" />
...
jQuery.ajax({
type: 'POST',
url: "${postAndView}",
data:myJSON,
contentType: "application/json",
success: function(data) {
previewDialog.html(data);
previewDialog.dialog('open');
}
});
我改为ajax调用,但jQuery.postJSON()
可能会起作用。下面显示的是新的控制器代码,它正确地将新对象添加到模型并返回jsp页面,该页面在对话框中打开:
@RequestMapping(value = "/PostJSONMAV", method = RequestMethod.POST)
public ModelAndView postJSON(@RequestBody MyClass myClass) {
ModelAndView mav = new ModelAndView();
myClass.setTitle("SUCCESS");
mav.setViewName("dialogContent");
mav.addObject("myClass", myClass);
return mav;
}