【发布时间】:2011-02-13 20:47:29
【问题描述】:
我正在使用 3.0.0.beta3 构建一个新应用。我只是尝试将 js.erb 模板呈现给 Ajax 请求以执行以下操作(在 Publications_controller.rb 中):
def get_pubmed_data
entry = Bio::PubMed.query(params[:pmid])# searches PubMed and get entry
@publication = Bio::MEDLINE.new(entry) # creates Bio::MEDLINE object from entry text
flash[:warning] = "No publication found."if @publication.title.blank? and @publication.authors.blank? and @publication.journal.blank?
respond_to do |format|
format.js
end
end
目前,我的 get_pubmed_data.js.erb 模板很简单
alert('<%= @publication.title %>')
服务器响应如下
alert('Evidence for a herpes simplex virus-specific factor controlling the transcription of deoxypyrimidine kinase.')
除了浏览器中没有发生任何事情之外,这非常好,可能是因为响应的内容类型是“text/html”而不是“text/javascript”,如此处部分复制的响应标头所示:
Status 200
Keep-Alive timeout=5, max=100
Connection Keep-Alive
Transfer-Encoding chunked
Content-Type text/html; charset=utf-8
这是一个错误还是我错过了什么?感谢您的帮助!
【问题讨论】:
-
你怎么称呼
get_pubmed_data?在 Rails 3 中你必须这样做:<%= link_to 'Click here', :controller => :publications, :action => :get_pubmed_data, :remote => true %> -
对 get_pubmed_data 的调用很好,通过 javascript observe_field 函数完成,带有正确的
-Requested-With XMLHttpRequestrequest Headers
标签: javascript ajax ruby-on-rails-3 content-type