Rails3呈现与文本/ html内容类型,而不是文本/ javascript中js.erb模板 [英] Rails3 renders a js.erb template with a text/html content-type instead of text/javascript

查看:130
本文介绍了Rails3呈现与文本/ html内容类型,而不是文本/ javascript中js.erb模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在建设有3.0.0.beta3一个新的应用程序。我只是试图呈现一个js.erb模板Ajax请求以下操作(在publications_controller.rb):

I'm building a new app with 3.0.0.beta3. I simply try to render a js.erb template to an Ajax request for the following action (in 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模板就是

Currently, my get_pubmed_data.js.erb template is simply

alert('<%= @publication.title %>')

服务器响应与以下

The server is responding with the following

alert('Evidence for a herpes simplex virus-specific factor controlling the transcription of deoxypyrimidine kinase.')

这是完全正常的,只是没有发生在浏览器中,可能是因为内容类型的响应是,而不是如图所示的响应头在这里部分转载文/ JavaScript的text / html的:

which is perfectly fine except that nothing happen in the browser, probably because the content-type of the response is 'text/html' instead of 'text/javascript' as shown by the response header partially reproduced here:

Status 200
Keep-Alive timeout=5, max=100
Connection Keep-Alive
Transfer-Encoding chunked
Content-Type text/html; charset=utf-8

这是一个错误还是我失去了一些东西?感谢您的帮助!

Is this a bug or am I missing something? Thanks for your help!

推荐答案

我终于能迫使它得到正确的内​​容类型的响应:

I finally was able to get the right content-type in the response by forcing it with:

respond_to do |format|
    format.js {render :content_type => 'text/javascript'}
end

这篇关于Rails3呈现与文本/ html内容类型,而不是文本/ javascript中js.erb模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆