Returning HTML in the JS portion of a respond_to block throws errors in IE
Posted
by Horace Loeb
on Stack Overflow
See other posts from Stack Overflow
or by Horace Loeb
Published on 2010-02-25T22:15:59Z
Indexed on
2010/03/11
20:14 UTC
Read the original article
Hit count: 273
Here's a common pattern in my controller actions:
respond_to do |format|
format.html {}
format.js {
render :layout => false
}
end
I.e., if the request is non-AJAX, I'll send the HTML content in a layout on a brand new page. If the request is AJAX, I'll send down the same content, but without a layout (so that it can be inserted into the existing page or put into a lightbox or whatever).
So I'm always returning HTML in the format.js
portion, yet Rails sets the Content-Type response header to text/javascript
. This causes IE to throw this fun little error message:
Of course I could set the content-type of the response every time I did this (or use an after_filter
or whatever), but it seems like I'm trying to do something relatively standard and I don't want to add additional boilerplate code.
How do I fix this problem? Alternatively, if the only way to fix the problem is to change the content-type of the response, what's the best way to achieve the behavior I want (i.e., sending down content with layout for non-AJAX and the same content without a layout for AJAX) without having to deal with these errors?
© Stack Overflow or respective owner