0

我有一些带有 jquery ui 的标签。每个链接都应该收到一个 json 响应,但我无法让它工作。这是活跃的:

<li class="ui-state-default ui-corner-top ui-tabs-active ui-state-active" role="tab" tabindex="0" aria-controls="ui-tabs-1" aria-labelledby="ui-id-1" aria-selected="true">
            <a href="/comments.json" class="ui-tabs-anchor" role="presentation" tabindex="-1" id="ui-id-1">comments</a>
          </li>

回应是:

[{"created_at":"2013-07-12T22:51:46Z","id":8,"programa_id":88,"sent_at":"2013-07-12T22:51:46Z","texto":"testing","updated_at":"2013-07-12T22:51:46Z","user_id":null}]

我尝试了几件事,但最好的方法是:

  $( "#tab_messages" ).tabs({
    heightStyle: "fill",
    beforeLoad: function( event, ui ) {
      ui.ajaxSettings.dataType = "json";
      ui.ajaxSettings.dataFilter = function(data) {
        var jsonData = $.parseJSON(data);
        alert(jsonData[0].texto);
        return  jsonData[1].texto;
      };

       ui.jqXHR.error(function() {
        ui.panel.html(
          "Couldn't load this tab. We'll try to fix this as soon as possible. " +
          "If this wouldn't be a demo." );
        });
    }});
  });

面板不加载(出现 jqXHR.error),但如果我返回 jsonData[1].texto,它可以工作

4

1 回答 1

0

我不知道这是否是最好的解决方案,但是如果我删除 ui.jqXHR.error 函数并从 dataFilter 中设置面板的 html,它似乎可以工作:

      ui.ajaxSettings.dataFilter = function(data) {
        var jsonData = $.parseJSON(data);
        var htmlpanel = "";
        $.each(jsonData, function(index, value){
          htmlpanel += "<li>" + value.texto + "</li>";
        });
        ui.panel.html(htmlpanel);
      };
于 2013-07-12T23:22:53.470 回答