2014-01-28 47 views
0

我有以下脚本。这个脚本是一个JSP具有其他(级联选择)后,填充一个国家,州和城市组合框spring ajax json - 动态下拉选择

The script is suppose to fetch the data from the controller and populate list of states for the country selected. The function is called alert("inside ajax") is displayed and calls the controller method too. The controller fetches the list of states sucessfully and returns the list of states as "Set". when the control is passed back to the json alert("received data") doesnot pop up and nothing happens after that. There are no errors displayed too. 

I have "json-lib-2.4-jdk15.jar" in my library. Do I need any more settings? or what is that I'm missing? 

杰森Java脚本如下

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#countryName').change(
     function() { 
      alert("inside ajax"); 
      $.getJSON('${findStateURL}', { 
       countryName : $(this).val(), 
       ajax : 'true' 
      }, function(data) { 
       alert("received data"); 
       alert(data); 
       var html = '<option value="">State</option>'; 
       var len = data.length; 
       alert(len); 
       for (var i = 0; i < len; i++) { 
        html += '<option value="' + data[i].name + '">' 
          + data[i].name + '</option>'; 
        alert(html); 
       } 
       html += '</option>'; 
       $('#states').html(html); 
      }); 
     }); 
    }); 

控制器方法我里面m呼叫是

@Controller 
    public class CountryController { 

     @Autowired 
     private CountryService countryService; 
     @RequestMapping(value = "/getStates.htm", method = RequestMethod.GET) 
     @ResponseBody public 
     Set<State> StatesForCountry(
     @RequestParam(value = "countryName", required = true) String countryName, Map<String, Object> map) { 
     System.out.println("countryName " + countryName); 
     System.out.println("Country Id "); 
     List<State> states = countryService.getAllStates(countryName); 
     Set<State> state_set = new HashSet<State>(states); 
     System.out.println(" no. of states set = " + state_set); 
     return state_set; 
     } 

控制器正在返回'set'中所需的值。但唯一的问题是我没有收到任何数据在 警报(“收到的数据”);

回答

0

重新缩进代码后,因为我们不是机器:

$(document).ready(function() { 
$('#countryName').change(
    function() { 
     alert("inside ajax"); 
     $.getJSON(
      '${findStateURL}', 
      { 
       countryName : $(this).val(), 
       ajax : 'true' 
      }, 
      function(data) { 
       alert("received data"); 
       alert(data); 
       var html = '<option value="">State</option>'; 
       var len = data.length; 
       alert(len); 
       for (var i = 0; i < len; i++) { 
        html += '<option value="' + data[i].name + '">' 
        + data[i].name + '</option>'; 
        alert(html); 
       } 
       html += '</option>'; 
       $('#states').html(html); 
      } 
     ); 
    } 
); 
}); 

如果我读的API Doc如果JSON文件包含语法错误,请求通常会失败默默。

我建议你得到你的控制器返回的JSON(通过示例通过Firebug)并将你的JSON放入在线验证器中。也许你会发现一个错误。

+0

我不使用萤火虫,也不熟悉它。如果你能发现我在代码中犯的错误,那对我会有帮助。 – sachin

+1

如果您开发Web应用程序,那么如果您使用Firefox或在Chrome中使用开发工具,则应该安装Firebug。您将在浏览器请求中看到错误。 你的代码对我来说似乎很好,这就是为什么我认为web应用程序返回的JSON可能存在错误。 – Mannekenpix