rails sending the wrong content-type in ajax respo

2019-07-24 09:04发布

I have been struggling terribly for the better part of the day with the following problem:

  • I have a rails 3.1 app - running fine
  • I am trying to add some ajax to it

However, no matter what I try, Rails insists on sending the wrong content-type in the response header.

#the form    
= form_for @signup, :url => '/signup', :remote => true do |f|
    = f.text_field :email, :class => 'email', :size => 26
    = f.submit 'Notify me'

#In the controller
def create
  respond_to do |format|
    format.js { render :content_type => 'text/javascript', :layout => false}
  end
end

#the JS file: create.js.erb
alert('HI!');

This is not working, while I think it should. Inspecting the server response with Firebug it tells me the response header has a content-type of text/html. I figure this is why it isn't working (as on a newly generated app, using the same jquery-rails gem it responds with a text/javascript header.

The server says it's responding with JS:

Processing by SignupsController#create as JS

But is somehow refusing to send the correct header. I am lost on where to look next.

Any help or direction as to where to look next would be greatly appreciated! Erwin

1条回答
叛逆
2楼-- · 2019-07-24 09:37

Ok found the solution,

So an after_filter in application controller was overriding the content-type. And they are processed inside out. So it was overwriting my declarations.

查看更多
登录 后发表回答