javascript 似乎没有从 rails 源获取数据

标签 javascript ruby-on-rails polling ajax-polling

我正在使用 dataTables 来显示一个漂亮的表格来显示各种信息。 javascript 成功地将轮询请求发送到数据库/网络服务器,但是 javascript 似乎没有获取任何当前数据。

这是我的 Controller

class CommentsController < ApplicationController
  before_filter :authenticate_user!
  before_filter :get_current_user

  # GET /comments
  # GET /comments.json

  def get_current_user
    @user=current_user
  end

  def index
    @comments = @user.comments

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @comments }
    end
  end

查看:

<h1>Listing comments</h1>

<table id="comments_id" class="display">
<thead>
  <tr>
    <th>String</th>
    <th>secondString</th>
    <th></th>
    <th></th>
  </tr>
</thead>
<tbody>
</tbody>
</table>

<br />

<%= link_to 'New Comment', new_comment_path %>
<h1>Listing comments</h1>

<table id="comments_id" class="display">
<thead>
  <tr>
    <th>State</th>
    <th>Environment</th>
    <th>dns</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
</thead>
<tbody>
</tbody>
</table>

<br />

<%= link_to 'New Comment', new_comment_path %>
<h1>Listing comments</h1>

<table id="comments_id" class="display">
<thead>
  <tr>
    <th>State</th>
    <th>Environment</th>
    <th>dns</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
</thead>
<tbody>
</tbody>
</table>

<br />

<%= link_to 'New Comment', new_comment_path %>

评论.js:

function InitOverviewDataTable()
{
  oOverviewTable =$('#desktops_id').dataTable(
  {
    "bPaginate": true,
    "bJQueryUI": true,  // ThemeRoller-stöd
    "bLengthChange": false,
    "bFilter": false,
    "bSort": false,
    "bInfo": true,
    "bAutoWidth": true,
    "bProcessing": true,
    "iDisplayLength": 10,
    "sAjaxSource": 'desktops'
  });
}

function RefreshTable(tableId, urlData)
{
  $.getJSON(urlData, null, function( json )
  {
    table = $(tableId).dataTable();
    oSettings = table.fnSettings();

    table.fnClearTable(this);

    for (var i=0; i<json.aaData.length; i++)
    {
      table.oApi._fnAddData(oSettings, json.aaData[i]);
    }

    oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
    table.fnDraw();
  });
}

function AutoReload()
{
  RefreshTable('#desktops_id', 'desktops');

  setTimeout(function(){AutoReload();}, 3000);
}

$(document).ready(function () {
  InitOverviewDataTable();
  setTimeout(function(){AutoReload();}, 3000);
});

我看到在数据库端推送了正确的查询:

Started GET "/comments" for 127.0.0.1 at 2013-01-22 18:46:16 -0500
Processing by CommentsController#index as JSON
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Comment Load (0.4ms)  SELECT "comments".* FROM "comments" WHERE "comments"."user_id" = 1
Completed 200 OK in 6ms (Views: 3.2ms | ActiveRecord: 0.9ms)

但是在 firefox 的网络控制台上,我看到它在轮询时没有检索到任何数据......并且当 dataTable 卡在“加载数据”时

[19:04:37.395] GET http://localhost:3000/comments?_=1358899476292 [HTTP/1.1 200 OK  38ms]
[19:04:39.344] GET http://localhost:3000/comments [HTTP/1.1 304 Not Modified  15ms]
[19:04:42.360] GET http://localhost:3000/comments [HTTP/1.1 304 Not Modified  18ms]

谢谢你的帮助

最佳答案

问题是来自您的服务器的 JSON 数据没有键 aaData 的值,因为您正在序列化 ActiveRecord 对象。

我通常会改变您使用 DataTable 插件的方式,因为它看起来像是您在使用内部 API(例如 _fnAddData),这有点令人讨厌。

相反,我会推荐 Ryan Bates 在其 RailsCast #340 中展示的方法 在数据表上。这种方式对我来说非常有效,我从来没有遇到过问题。

关于javascript 似乎没有从 rails 源获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14470314/

相关文章:

php - Laravel 中的长轮询(sleep() 函数使应用程序卡住)

javascript - 选项卡式工具栏中的 Sencha Touch 列表组件

javascript - 特定虚拟页面 View 上的火灾事件

javascript - 如何从 div 中获取文本?

ruby-on-rails - Windows 7 上的 Docker Compose : Could not locate Gemfile or . bundle /目录

git - Jenkins PollScm 用于特定存储库

BizTalk - 接收端口从数据库读取两次

javascript - 如何从定制的 ACE 编辑器模式强制软包装?

ruby-on-rails - 如何让Minitest错误代码也返回错误信息?

ruby-on-rails - 如何安全地管理一次性脚本以修复数据?