在document ready中调用getJSON静默失败

call to getJSON in document ready failing silently

本文关键字:getJSON 静默 失败 调用 document ready      更新时间:2023-09-26

我在文档中准备了两次调用getJSON,都是为了在页面上填充不同的组合框。第一个工作没有问题。第二个不填充控件,但我可以从我的日志中看到,它已经从模型请求数据,控制器也获得数据。

        $.getJSON(
                    '<%= pathtomethod%>',
                    function(data) {
                        data = $.parseJSON(data); //converting to a javascript object vs. just string....
                      //  console.log(data);  
                        if (data !=null){
                                $.each(data, function(i) {
                                  $('#ruletypes')
                                         .append($("<option></option>")
                                         .attr("value",this.id)
                                         .text(this.name)); 
                            }); //end .each                             
                        }//end if
                    }//end function(data)
        );//end getJSON.        
        //populate rule conditions drop down box.
        $.getJSON(
                    '<%= pathtomethod%>',
                    function(data) {
                        data = $.parseJSON(data); //converting to a javascript object vs. just string....
                         console.log(data);
                        if (data != null) {
                        $.each(data, function(i) {
                            $('#rule_condition')
                                 .append($("<option></option>")
                                 .attr("value",this.id)
                                 .text(this.description)); 
                        }); //end .each
                    }//end if
                    }//end function(data)
        );//end getJSON.    

控制台中没有出现错误。但也没有数据出现……下面是rule_condition下拉列表的html:

        <div class="first">
            <select id='rule_condition' name='rule_condition'>
            </select> 
        </div>

编辑1

我找到了罪魁祸首。在第二个请求中返回的数据包含单引号。现在我只是在谷歌上查找如何处理json数据中的嵌入引号

您应该尝试实现成功和错误函数,并观察错误是什么。另外,尝试使用firebug