微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Rails3使用text / html内容类型而不是text / javascript呈现js.erb模板

我正在用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

这是一个错误还是我错过了什么?谢谢你的帮助!

解决方法

我终于能够通过强制它来获得响应中的正确内容类型:
respond_to do |format|
    format.js {render :content_type => 'text/javascript'}
end

原文地址:https://www.jb51.cc/html/227415.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐