How do I return a view from a spring controller using an ajax request?

后端 未结 1 1309
灰色年华
灰色年华 2020-12-12 01:03

I make an ajax request using jquery, this calls the following spring controller:

@RequestMapping(value = \"/dialogController\", method = RequestMethod.POST)
         


        
相关标签:
1条回答
  • 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;     
    }
    
    0 讨论(0)
提交回复
热议问题