Javascript spring ajax json-动态下拉选择

Javascript spring ajax json-动态下拉选择,javascript,ajax,json,spring,jackson,Javascript,Ajax,Json,Spring,Jackson,我有以下脚本。此脚本位于一个JSP中,其中包含一个接一个填充的国家、州和城市组合框(级联选择) jasonjava脚本如下所示 <script type="text/javascript"> $(document).ready(function() { $('#countryName').change( function() { alert("inside ajax"); $.get

我有以下脚本。此脚本位于一个JSP中,其中包含一个接一个填充的国家、州和城市组合框(级联选择)

jasonjava脚本如下所示

 <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); 
             });
         }); 
    });

$(文档).ready(函数(){
$(“#countryName”)。更改(
函数(){
警报(“内部ajax”);
$.getJSON(“${findStateURL}”{
countryName:$(this).val(),
阿贾克斯:“真的”
},函数(数据){
警报(“收到的数据”);
警报(数据);
var html='State';
var len=data.length;
警报(len);
对于(变量i=0;i
我调用的控制器方法是

@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;
         }
@控制器
公共类控制器{
@自动连线
私人乡村服务;
@RequestMapping(value=“/getStates.htm”,method=RequestMethod.GET)
@负责人公众
设置StatesForCountry(
@RequestParam(value=“countryName”,required=true)字符串countryName,映射){
System.out.println(“countryName”+countryName);
System.out.println(“国家Id”);
列表状态=countryService.GetAllState(countryName);
Set state_Set=新哈希集(states);
System.out.println(“状态集的数量=”+状态集);
返回状态集;
}
控制器正在返回“集合”中所需的值。但唯一的问题是我当时没有收到任何数据
重新识别代码后发出警报(“收到的数据”);

,因为我们不是机器:

$(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); 
            }
        );
    }
 ); 
});
$(文档).ready(函数(){
$(“#countryName”)。更改(
函数(){
警报(“内部ajax”);
$.getJSON(
“${findStateURL}”,
{
countryName:$(this).val(),
阿贾克斯:“真的”
}, 
功能(数据){
警报(“收到的数据”);
警报(数据);
var html='State';
var len=data.length;
警报(len);
对于(变量i=0;i
如果我读取:如果JSON文件包含语法错误,请求通常会自动失败。


我建议您获取控制器返回的JSON(通过Firebug示例)并将您的JSON放入在线验证程序中。也许您会发现错误。

我不使用firebug,也不熟悉它。如果您能在代码中发现我犯的错误,这将对我很有帮助。如果您开发web应用程序,如果您使用Firefox或使用Chrome中的开发工具,您应该安装firebug。您将在您的浏览器中看到错误er请求。您的代码对我来说似乎很好,这就是为什么我认为webapp返回的JSON中可能有错误。
$(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); 
            }
        );
    }
 ); 
});