简体   繁体   中英

How to handle ajax success method return model in java script

i am creating ajax call on Change on first drop down.Based on First drop down i am getting second drop down values.Though Ui i am sending selected customer name and trigger post method.Through back end i am return model object.How to handle this model in ajax success call.

<script>
    $(document).ready(function() {
        $("#customerDetails").change(function() {
            var value1 = $('#customerDetails :selected').text();
            $.ajax({
                type : 'POST',
                url : 'environments',
                data : {
                    selectedcustomername : value1
                },
                success : function(result) {
                    //how to handle model in this 
               }
            });
        });
    });
</script>

This is controller layer code:

public ModelAndView getenvironments(HttpServletRequest request,
            HttpServletResponse response,@ModelAttribute("customer") Customer customer,@RequestParam String selectedcustomername) throws Exception{
        System.out.println("selected cust name"+selectedcustomername);
        ModelAndView model = null;  
        Map<String, Object> map = new HashMap<String, Object>();
        List<org.mvc.domain.Customer> customerList = loginDelegate.getCustomerList();
        List<org.mvc.domain.Environments> environmentnamesList = loginDelegate.getEnvironments(selectedcustomername);
        Collections.sort(environmentnamesList, new CustomComparator());
        for(int i=0;i<environmentnamesList.size()-1;i++){
            customer.setEnvrironmentId(environmentnamesList.get(0));
            customer.setEnvironmentName(environmentnamesList.get(1));
        }
        map.put("customerList", customerList);
        map.put("environmentnamesList", environmentnamesList);
        model = new ModelAndView("welcome", "map", map);
        return model;

    }

This is my drop down ui code

<form:form method="get" commandName="frmSample" action="retrieve" modelAttribute="customer">
<b>Environment:</b>
                        <form:select path="EnvrironmentId" id="environmentName">                                                                                                                                                    
                        <option selected="selected" disabled="disabled">Select An Environment</option>
                        <form:options items="${map.environmentnamesList}" itemLabel="environmentName" itemValue="envrironmentId"/>                                                                                                                                                      
                        </form:select>
                     </td>

My First dropdown Ui:

<form:select path="CustomerId" id="customerDetails">
    <option selected="selected" disabled="disabled">Select Customer</option>
     <form:options items="${map.customerList}"
     itemLabel="customerName" itemValue="customerId" />
    </form:select> <br>


@RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView executeLogin(HttpServletRequest request,
            HttpServletResponse response,
            @ModelAttribute("loginBean") LoginBean loginBean,
            @ModelAttribute Customer customer) {
        response.setHeader("Connection", "Keep-Alive");
        ModelAndView model = null;
        try {
            boolean isValidUser = loginDelegate.isValidUser(
                    loginBean.getUsername(), loginBean.getPassword());
            if (isValidUser) {
                System.out.println("User Login Successful");
                request.setAttribute("loggedInUser", loginBean.getUsername());
                Map<String, Object> map = new HashMap<String, Object>();
                List<org.mvc.domain.Customer> customerList = loginDelegate
                        .getCustomerList();
                map.put("customerList", customerList);
                model = new ModelAndView("welcome", "map", map);
            } else {

                model.addObject("loginBean", loginBean);
                request.setAttribute("message", "Invalid credentials!!");
                model = new ModelAndView("login");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return model;
    }

For ModelAndView return type is used ModelAndViewResolverMethodReturnValueHandler, so it will not return you an json string. I suggest you return a Map, and use the @ResponseBody like:

public @ResponseBody Map<String, Object> getEnvironments(HttpServletRequest request,
            HttpServletResponse response,@ModelAttribute("customer") Customer customer,@RequestParam String selectedcustomername) throws Exception{
        System.out.println("selected cust name"+selectedcustomername);

        Map<String, Object> map = new HashMap<String, Object>();
        List<org.mvc.domain.Customer> customerList = loginDelegate.getCustomerList();
        List<org.mvc.domain.Environments> environmentnamesList = loginDelegate.getEnvironments(selectedcustomername);
        Collections.sort(environmentnamesList, new CustomComparator());
        for(int i=0;i<environmentnamesList.size()-1;i++){
            customer.setEnvrironmentId(environmentnamesList.get(0));
            customer.setEnvironmentName(environmentnamesList.get(1));
        }
        map.put("customerList", customerList);
        map.put("environmentnamesList", environmentnamesList);
        return map;

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM