javascript没有';t似乎从rails源获取数据
我使用dataTables来显示一个漂亮的表来显示各种信息。javascript成功地向数据库/Web服务器发送轮询请求,但是javascript似乎没有获取任何当前数据 这是我的控制器javascript没有';t似乎从rails源获取数据,javascript,ruby-on-rails,polling,ajax-polling,Javascript,Ruby On Rails,Polling,Ajax Polling,我使用dataTables来显示一个漂亮的表来显示各种信息。javascript成功地向数据库/Web服务器发送轮询请求,但是javascript似乎没有获取任何当前数据 这是我的控制器 class CommentsController < ApplicationController before_filter :authenticate_user! before_filter :get_current_user # GET /comments # GET /commen
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
然而,在firefox的web控制台上,我看到当它进行轮询时,没有检索到任何数据……当dataTable陷入“加载数据”状态时
感谢您的帮助问题在于,来自服务器的JSON数据没有键
aaData
的值,因为您正在序列化ActiveRecord
对象
我通常会改变使用DataTable插件的方式,因为它看起来像是在使用内部API(例如,\u fnAddData
),这有点令人讨厌
相反,我推荐Ryan Bates在其关于数据表的中所展示的方法。这种方法对我来说非常有效,我从来没有遇到过问题。您能提供
json
变量的控制台日志转储吗?它是否包含键aaData
?我运行了firebug并找到了这个。。。。。。。。。。。TypeError:json.aaData未定义[Break On This Error]for(var i=0;我看了一下这个问题,特别是带有fnReloadAjax()定义的问题:
<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 %>
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)
[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]