HTML   发布时间:2022-04-14  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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
@H_772_4@目前,我的get_pubmed_data.js.erb模板很简单

alert('<%= @publication.title %>')
@H_772_4@服务器正在响应以下内容

alert('Evidence for a herpes simplex virus-specific factor controlling the transcription of deoxypyrimidine kinase.')
@H_772_4@除了在浏览器中没有任何反应之外,这是完全正常的,可能是因为响应的内容类型是’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
@H_772_4@这是一个错误还是我错过了什么?谢谢你的帮助!

解决方法

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

大佬总结

以上是大佬教程为你收集整理的Rails3使用text / html内容类型而不是text / javascript呈现js.erb模板全部内容,希望文章能够帮你解决Rails3使用text / html内容类型而不是text / javascript呈现js.erb模板所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。