我如何返回使用一个Ajax请求弹簧控制器的看法? [英] How do I return a view from a spring controller using an ajax request?
问题描述
我使用jQuery做一个Ajax请求,这调用下面的弹簧控制器:
@RequestMapping(值=/ dialogController,方法= RequestMethod.POST)
公共字符串dialogController(型号机型,@RequestBody MyClass的MyClass的){
myClass.setTitle(成功);
model.addAttribute(MyClass的,MyClass的);
返回dialogContent; //解析为dialogContent.jsp
}
不过,我收到以下错误:
org.springframework.web.Htt prequestMethodNotSupportedException:
不支持请求方法POST
如果需要的话在这里就是Ajax调用我使用jQuery做:
jq.postJSON(/ dialogController,MyClass的,功能(数据){
myDialog.html(数据);
myDialog.dialog(开放);
//对话框设置previously分配,
//但是成功的回调函数,无论如何都不会达到
});
修改的 我得到同样的错误,如果我使用:
jq.ajax({
键入:POST,
网址:/ dialogController
数据:MyClass的,
成功:功能(数据){
previewDialog.html(数据);
previewDialog.dialog('开');
});
对于观众在家中......我发现这个问题是由于控制器的定义不匹配的Ajax调用的方法签名。我删除了从控制器方法的型号model
参数。我也便意识到我必须也返回一个新的模型和视图;这里是工作code:
VAR myJSON = {称号:帮助};
myJSON = JSON.stringify(myJSON);
c为C:VAR URL =postAndView值=/ PostJSONMAV/>
...
jQuery.ajax({
键入:POST,
网址:$ {postAndView},
数据:myJSON,
的contentType:应用/ JSON
成功:功能(数据){
previewDialog.html(数据);
previewDialog.dialog('开');
}
});
我改成Ajax调用,但 jQuery.postJSON()
可能会工作以及。下面显示的是新的控制器code,这corrrectly增加了一个新的对象模型,并返回jsp页面,它开辟了一个对话框:
@RequestMapping(值=/ PostJSONMAV,方法= RequestMethod.POST)
公众的ModelAndView postJSON(@RequestBody MyClass的MyClass的){
ModelAndView的MAV =新的ModelAndView();
myClass.setTitle(成功);
mav.setViewName(dialogContent);
mav.addObject(MyClass的,MyClass的);
返回MAV;
}
I make an ajax request using jquery, this calls the following spring controller:
@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
}
However I receive the following error :
org.springframework.web.HttpRequestMethodNotSupportedException:
Request method 'POST' not supported
And if required here is the ajax call I am making using jQuery:
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
});
EDIT I get same error if I use :
jq.ajax({
type: 'POST',
url: "/dialogController",
data:myClass,
success: function(data) {
previewDialog.html(data);
previewDialog.dialog('open');
});
For the viewers at home ... I found that the problem was due to the method signature defined in controller not matching the ajax call. I removed the Model model
parameter from controller method. I also then realized I had to also return a new model and view; here is the working code:
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');
}
});
I changed to the ajax call but jQuery.postJSON()
will probably work aswell. And shown below is the new controller code, which corrrectly adds a new object to model and returns jsp page, which is opened up in a dialog:
@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;
}
这篇关于我如何返回使用一个Ajax请求弹簧控制器的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!