我正在使用 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”,如此处部分复制的响应 header 所示:
Status 200
Keep-Alive timeout=5, max=100
Connection Keep-Alive
Transfer-Encoding chunked
Content-Type text/html; charset=utf-8
这是一个错误还是我遗漏了什么?感谢您的帮助!
最佳答案
我终于能够通过以下方式在响应中获得正确的内容类型:
respond_to do |format|
format.js {render :content_type => 'text/javascript'}
end
关于javascript - Rails3 使用 text/html 内容类型而不是 text/javascript 呈现 js.erb 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2738361/