How do I return a view from a spring controller using an aja

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.jspHowever I receive the following error :org.springframework.web.HttpRequestMethodNotSupportedException: Request method ‘POST’ not supportedAnd if …

via Java Application Development Tutorial » Search Results » ajax:

How do I return a view from a spring controller using an aja

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



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;

For more info: How do I return a view from a spring controller using an aja

Java Application Development Tutorial » Search Results » ajax

How do I return a view from a spring controller using an aja

Share this post:

Related Posts

Leave a Comment