【问题标题】:spring ajax json - dynamic drop down selectspring ajax json - 动态下拉选择
【发布时间】:2014-01-28 05:28:51
【问题描述】:

我有以下脚本。此脚本位于 JSP 中,其中一个接一个地填充了 Country、State 和 Cities 组合框(级联选择)

 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?

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

我调用的控制器方法是

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

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

【问题讨论】:

    标签: javascript ajax json spring jackson


    【解决方案1】:

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

    $(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 放入在线验证器中。也许你会发现一个错误。

    【讨论】:

    • 我不使用firebug,也不熟悉。如果您能发现我在代码中所犯的错误,那将对我有所帮助。
    • 如果您开发Web应用程序,如果您使用Firefox或使用Chrome中的开发工具,则应该安装Firebug。您将在浏览器请求中看到错误。您的代码对我来说似乎很好,这就是为什么我认为 webapp 返回的 JSON 中可能存在错误。
    猜你喜欢
    • 2023-03-05
    • 2011-11-12
    • 2019-06-04
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 2020-01-06
    相关资源
    最近更新 更多