Spring Modelandview not working properly with AJAX callback

Asked

Viewed 121 times

0

Well, some time ago I made a feature on my site consisting of performing a filter (called AJAX), and in my action I simply return a view (at the moment I’m rendering via callback, but if possible I would like to own Modelandview was responsible for this).

Calling for 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 in 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, that approach even works through callback ($('html').html( response );), but the problem is that the page renders with layout broken and not counting some functions jQuery do not work perfectly.

Is there any other possibility? Would I avoid the callback leaving the responsibility to the Tilesviewresolver accomplish this?

1 answer

0

In this case, switching to solve and playing the responsibility for Tiles, you will continue to return an html. Perhaps the best way would be to return a JSON with the destinations. To do this, write down your method with @Responsebody and directly return the list of destinations. You’ll probably have to add the Jackson dependency (json serializer) to your project as well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.