【发布时间】:2016-06-09 21:15:53
【问题描述】:
我正在使用 Dart 作为客户端代码在我的 Rails 网站上进行无限滚动。我找到了一个调用示例,令人惊讶的是,它第一次就奏效了。问题是它返回的 html 与我单击下一个按钮时得到的相同。我不需要整个页面,只需要滚动数据。我决定尝试 JSON。该请求由 Rails 处理,但仍将其呈现为 html。这是我所做的更改:
html 请求
HttpRequest.request(url).then(onDataLoaded);
json 请求
HttpRequest.request(url, responseType: 'json').then(onDataLoaded);
变量 url 具有来自“下一步”按钮的 url 副本:
http://jazzcat.loc/artists?page=2
当我打印到浏览器控制台时,使用此响应处理程序:
void onDataLoaded(HttpRequest resp) {
print(resp.statusText);
print(resp.responseHeaders);
print(resp.response);
}
状态文本正常。 响应头是:
{x-runtime: 0.049101, date: Thu, 09 Jun 2016 23:51:05 GMT, x-content-type-options: nosniff, server: Apache/2.4.18 (Unix) Phusion_Passenger/5.0.24 PHP/5.5.34, x-powered-by: Phusion Passenger 5.0.24, x-frame-options: SAMEORIGIN, content-type: text/html; charset=utf-8, status: 200 OK, cache-control: max-age=0, private, must-revalidate, transfer-encoding: chunked, etag: W/"fb290bd42f831f80ea9359040304ea39", connection: Keep-Alive, keep-alive: timeout=5, max=100, x-xss-protection: 1; mode=block, x-request-id: 9921c6c7-2fb6-43b9-89d3-636fb4a3aec0}
rest.response 为空。
我在 Chrome 浏览器中检查了网络数据,并在响应选项卡上看到了完整的 html 数据。 Dart代码一定认为是json,无法显示
这是来自 json 响应类型请求的 Rails 应用程序日志:
Started GET "/artists?page=2" for 127.0.0.1 at 2016-06-10 17:05:06 -0700
Processing by ArtistsController#index as */*
Parameters: {"page"=>"2"}
ArtistsController index method
Rendered artists/_list_subnav.html.erb (0.2ms)
Artist Load (1.3ms) SELECT `artists`.* FROM `artists` ORDER BY last_name, first_name LIMIT 25 OFFSET 25
(0.4ms) SELECT COUNT(*) FROM `artists`
Rendered artists/index.html.erb within layouts/application (22.9ms)
Completed 200 OK in 31ms (Views: 28.6ms | ActiveRecord: 1.7ms)
我有一个 index.json.erb,我也尝试将它添加到控制器:
def index
# Perform any filtering using ransack search
logger.debug "ArtistsController index method format"
@q = Artist.search(params[:q])
# Partition the output using kaminari page
@artists = @q.result.order('last_name', 'first_name').page(params[:page])
respond_to do |format|
format.html
format.json { render :json => @artists }
end
end
如何让 Rails 呈现 json?上面代码中的格式是如何设置的?
更新
我已经完全重写了这个问题。
【问题讨论】:
标签: ruby-on-rails json ajax dart