2

I have two dropdownlists in Jsp One for state and other for country. As soon as i select country the statelist should be populated automatically with respective lists. But i am getting whole jsp page as response in ajax call. My ajax Program:

$("select#country").change(function() {
var val=$("#country").val();
alert(val);
$.ajax({
url : 'getstates',
method : 'get',
contentType: 'application/json',
 data :{
          country : val
        },

  success: function (data) {
  alert("Success Response"+ data);

  },

   error :function()
   {
           alert("error");
    }          

   });

My controller program

 @RequestMapping(value = "/getstates", method = RequestMethod.GET)
 public ModelAndView  showstates(@RequestParam(required = false, value = "") 
     String country,@Valid    @ModelAttribute("employee")Login employee, 
     BindingResult result, Model model) {  
    HashMap<String,String>  stateMap = new HashMap<String,String>();
     //put your logic to add state on basis of country

     if (country.equals("UnitedStates")) {
         stateMap.put("Al","Alaska");
         stateMap.put("Tl","Texas");

     } else if (country.equals("UnitedKingdom")) {
         stateMap.put("Be","Bedfordshire");
         stateMap.put("Ber","Berkshire");

     } else if (country.equals("India")) {
         stateMap.put("Mh","Maharashtra");
         stateMap.put("WB","West Bengal");
         stateMap.put("KR","Karnataka");
         stateMap.put("AP","Andhra Pradesh");
         stateMap.put("TN","Tamil Nadu");
     } 


     return new ModelAndView("LoginForm","state" ,stateMap);
 }

I am using spring form. I need to get only Staemap as respone but i am getting whole jsp page as response.

Niketa
  • 453
  • 2
  • 9
  • 24

2 Answers2

1

I need to get only Staemap as respone but i am getting whole jsp page as response.

Because you are returning the ModelAndView object with the view here,

return new ModelAndView("LoginForm","state" ,stateMap);

If you need to return the respone alone from the controller method.However you cant print the HashMap directly in the ajax response on your jsp. IMHO you can convert it to JSONArray

JSONArray jarray = JSONArray.fromObject(statemap);

Read ,

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • Thank u.. should i return Json array to jsp? but how to access jarray in jsp? – Niketa Nov 07 '14 at 10:32
  • Added the examples in my answer. you can't directly access them in jsp , need javascript to do the work in ajax – Santhosh Nov 07 '14 at 10:35
  • I am using spring form and jquery. How to access hashmap in jquery? i mean hashmap is sent as response so how to access and put to dropdown menu? – Niketa Nov 08 '14 at 04:54
  • I am using spring form and jquery. Should i return only JSONArray from controller? how to access Jsonarray in jquery? – Niketa Nov 08 '14 at 05:00
0
  @RequestMapping(value="LoadBaselineVersions")
        @ResponseBody
        public Map<Integer,String> loadBaseNames(@RequestParam(value="projectname") String name,HttpSession session){

                 return basenameService.getBaselineversions(name);
        }
$("#projectname").bind(
                'blur',
                function() {
                    $.ajax({
                        type : 'post',
                        url : 'LoadBaselineVersions?projectname='
                                + $("#projectname").val(),
                        dataType : 'json',
                        success : function(data) {
                            $("#baseversion").empty();
                            $.each(data, function(val, text) {

                                $("#baseversion").append(
                                        $('<option></option>').val(text).html(
                                                text));

                            });

                        }
                    });
                });
Selva
  • 1,620
  • 3
  • 33
  • 63