Topic: Ensuring correct content-type with jQuery 1.4

I'm trying to upgrade my application to use jQuery 1.4.2 and I'm having a bit of a problem with rails returning the required content-type header for the response.

Here is the break down of what is happening:

1. I'm setting all ajax requests to use the "text/javascript" request header when sending an ajax request.

jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) { xhr.setRequestHeader("Accept", "text/javascript"); } 
});

2. The $.get() request is made using the following line (from the Facebox jQuery plugin)

$.get(href, function(data) { $.facebox.reveal(data, klass); });

3. The request hits the controller and executes the format.js block

respond_to do |format|
  format.js { render :template => "addresses/new.html.haml", :layout => false }
end

4. The response is sent back to the browser with the response content-type set to "text/javascript"

5. jQuery then checks the content-type to see how it should handle the returned data (this is new to 1.4+)

6. Because the content-type is set to "text/javascript" jQuery tries to eval the code.

The problem is I want the response content-type to be set to "text/html" so jQuery will not eval the returned html.

Can anyone see a way to get this working as needed?

Thanks,
Scott

Re: Ensuring correct content-type with jQuery 1.4

I think you need to rewrite your request to use jQuery.ajax so you can use the `dataType: "html"` option.  I was under the impression you could put it in your jQuery.ajaxSetup object, but I am having some trouble with that (I'm using .load and I'm have to put .js on my url. It might just be time to go to bed.)

$.ajax({
  url: href,
  success: function(data) { $.facebox.reveal(data, klass); },
  dataType: 'html'
});

Re: Ensuring correct content-type with jQuery 1.4

Based on the jQuery docs what  GhettoDuk is saying looks like it might be correct.

The problem is I don't really want to specify the type of data I'm going to receive when I'm making the Ajax call. As you can see the Facebox plugin just makes a $.get() request without specifying the expected data type it should receive.

Ideally I should just be able to make an Ajax request and then the controller should be able to send back anything it wants. That way if my controller is going to send back JSON I can handle that but if it is going to send back HTML I can handle that as well.

It still seems like a limitation of rails not to be able to receive an "text/javascript" request and then specify the respone to be "text/javascript".

Am I missing something here?

Re: Ensuring correct content-type with jQuery 1.4

Try adding this to your render:

:content_type => "application/x-www-form-urlencoded"

Re: Ensuring correct content-type with jQuery 1.4

Thanks for the response, I'll have it wait till I get to work on Monday to try it out.

I did try the following yesterday but it didn't work either

format.js { render :template => "addresses/new.html.haml", :layout => false, :content_type => :html }

Re: Ensuring correct content-type with jQuery 1.4

If you send a request with the Accept header of 'text/javascript' and let rails respond with format.js, then jQuery 1.4.3 (and later I think) will try to eval the response, since it assumes it is javascript. You may think doing something like this will fix it:

$.ajax('/whatever', {
  dataType: 'html',
  beforeSend: function(xhr) {
    xhr.setRequestHeader('Accept', 'text/javascript');
  }
});

but, this breaks in IE because jQuery appends an accept header of */* which triggers rails to render the format.html block instead. This only appeared to happen for me in IE.

The fix I found was to trick jQuery into thinking that asking for HTML was the same as asking for Javascript.

$.ajaxSettings.accepts.html = $.ajaxSettings.accepts.script;
$.ajax('/whatever', {
  dataType: 'html'
});

This tells jQuery that you are getting back HTML, so it won't try to eval it, but tells rails that you want Javascript, so it hits the right format (since it sends accept headers of text/javascript).