getJSON不适用于最新版本的jQuery

时间:2012-01-12 16:26:07

标签: jquery getjson

以下代码片段使用jQuery1.2.3可以正常工作,但它不适用于最新版本的jQuery:

$.getJSON(url,{str: $$.val() }, function(j){
   if (j.length > 0) {
      var options = '<option value="">' +params.firstOption+ '</option>';
      for (var i = 0; i < j.length; i++) {
         options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
      }
   }
   $dest.removeAttr('disabled')
        .html(options)
        .find('option:first')
        .attr('selected', 'selected');
});

请注意,上面的代码实际上是用于级联下拉列表的jQuery插件的一部分。如果我使用jQuery1.2.3,它会产生所需的结果。完整的插件代码如下:

(function($){

   $.fn.linkedSelect = function(url,destination,params) {


       var params = $.extend({

         firstOption : 'Please Select',

         loadingText : 'Loading...'

      },params);

      var $dest = $(destination);

      return this.each(function(){

         $(this).bind('change', function() {

            var $$ = $(this);

            $dest.attr('disabled','false')
                 .append('<option value="">' +params.loadingText+ '</option>')
                 .ajaxStart(function(){

                    $$.show();

            });

            $.getJSON(url,{str: $$.val() }, function(j){
               alert('User clicked on this.'); 
               if (j.length > 0) {

                  var options = '<option value="">' +params.firstOption+ '</option>';

                  for (var i = 0; i < j.length; i++) {

                     options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';

                  }

               }

               $dest.removeAttr('disabled')
                    .html(options)
                    .find('option:first')
                    .attr('selected', 'selected');

            }); // end getJSON

         });  // end change

      }); // end return each

   };  // end function

})(jQuery);

请注意,它无法生成以下警告消息

alert('User clicked on this.');

如果我使用最新版本的jQuery,那么在getJSON函数中编写的内容用于调试目的。我还使用JS调试器进行了跟踪,如果我使用最新版本的jQuery,它就无法进入getJSON函数。但是,如果我使用jQuery1.2.3,它会显示此警告消息。

在错误控制台中,警告消息为:

Warning: reference to undefined property b.p.height
Source File: http://localhost//js/jquery.jqGrid.min.js Line: 99
Warning: reference to undefined property b.p.serializeGridData
Source File: http://localhost/js/jquery.jqGrid.min.js Line: 62
Warning: reference to undefined property jQuery.event.triggered
Source File: http://localhost/js/jquery1.7.js Line: 2924
Warning: reference to undefined property elem[jQuery.expando]
Source File: http://localhost/js/jquery1.7.js Line: 1719

我应该更改以适应最新版本的jQuery。谢谢。

2 个答案:

答案 0 :(得分:2)

根据控制台中的错误,您使用的jqGrid版本看起来与jQuery 1.7不兼容。

如果它与jQuery 1.2.3使用的jqGrid版本相同,那么,至少这并不奇怪。 jQuery 1.2.3是古老的历史 - 大约四年前发布。

答案 1 :(得分:0)

很可能您的服务器生成了无效的JSON。

相关问题