Spring ModelAndView not working correctly with AJAX callback

0

Well, sometime ago I did a feature on my website which consists of performing a filter (called AJAX ), and in my action I simply return a view I am rendering via callback , but if it is possible I would like the ModelAndView to be responsible for that.)

Call AJAX :

function perfectDestinationFilter(){
    $.ajax({
        type: "GET",
        url: "/viatge/perfect-travel-filter",
        data: $('#form-filter-perfect-travel').serialize(),
        success: function (response) {
            $('html').html( response );
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(thrownError);
        }
    });
}

Action responsible return my page with the destinations found:

@RequestMapping(value = "/perfect-travel-filter", method = RequestMethod.GET)
    public ModelAndView getDestinations(@RequestParam String social, @RequestParam String economic, @RequestParam String trip, @RequestParam String weather, @RequestParam String general, Model model) throws SQLException{
        if(Strings.isNullOrEmpty(social) || Strings.isNullOrEmpty(economic) || Strings.isNullOrEmpty(trip) || Strings.isNullOrEmpty(weather) || Strings.isNullOrEmpty(general)){
            return new ModelAndView("site/destinations02", "perfectDestinationError", true);
        }else{
            return new ModelAndView("site/destinations02", "perfectDestinations", destinationFacade.filterDestinations(economic, general, social, weather, trip));
        }

    }

Well, this approach even works through callback ( $('html').html( response ); ), but the problem is that the page renders with broken layout jQuery does not work perfectly.

Is there any other possibility? Would I have avoided the callback leaving the responsibility for the TilesViewResolver to accomplish this?

    
asked by anonymous 18.06.2015 / 03:41

1 answer

0

In this case, switching from resolving and throwing the responsibility to the tiles, you will continue to return an html. Perhaps the best way would be to return a JSON with the destinations. To do this, note your method with @ResponseBody and return directly to the list of targets. You'll probably have to add Jackson's dependency (json serializer) on your project as well.

    
23.06.2015 / 14:16