Returning HTML in the JS portion of a respond_to block throws errors in IE Returning HTML in the JS portion of a respond_to block throws errors in IE ajax ajax

Returning HTML in the JS portion of a respond_to block throws errors in IE


Does this work for you?

respond_to do |format|  format.html { :layout => false if request.xhr? }  format.js {}end

And then call the response as HTML, not JS(I have not tested it though.)

Basically, request.xhr? is the key for this solution


The problem is that the response is sending the Content-Type header as text/javascript which IE then tries to interpret as javascript, hence the missing ')' error message. You need the server to send response as type text/html so that the browser will not attempt to parse and execute the response content as a script but will allow you to use it as a block of HTML.

You can do this in Rails by adding something like the following to one of your controllers:

@headers["Content-Type"] = "text/html"

For example, you might add this to your Application Controller as follows:

class ApplicationController < ActionController::Base  before_filter :fix_ct  def fix_ct    @headers["Content-Type"] = "text/html"   end    end


IE doesn't understand the response as HTML. So either you change the header of the response, or test change the request method to GET.