Question

I have a simple form for submitting an image like so:

<%= form_tag avatar_item_path(@tutorial, :format => :js), :method => :post, :remote => true do %>
  <%= file_field_tag :item_avatar, :name => "item_image[image]" %>
<% end %>

(Dragonfly handles the image, in case that's somehow relevant)

My controller action is called update_avatar and it has no render or respond_to logic (it just calls find and save methods) so by default on a js request update_avatar.js.erb is rendered.

update_avatar.js.erb contains a js function and $(function () { // some code here })

In FF, Chrome and Safari this works fine.

The problem: IE9 wants to save the response as avatar.js

Using the developer tools I can see that the request is sent and received correctly and the response content-type is "text/javascript; charset=utf-8"

I've tried manually setting the content-type to application/javascript and this had no effect.

As others have suggested elsewhere, if I set the content-type to text/html IE9 will not prompt me to save it but in this case none of the browsers will actually evaluate the js in the response (I put the js in script tags).

From searching it seems that about a year ago others were experiencing the same issue and their solution was to upgrade jquery, since a year has passed I have a much newer version. Perhaps this is a regression?

I'm using the latest jquery-rails gem , 1.0.19, which includes jquery 1.7.1 and also includes the latest version of https://github.com/rails/jquery-ujs (I did a diff to make sure).

Was it helpful?

Solution

Using Jquery File Upload

https://github.com/blueimp/jQuery-File-Upload (basic version, not UI version)

Make sure the url has the js format explicitly stated and set the dataType option to 'script':

$('#item_avatar').fileupload({
  url: "<%= avatar_item_path(item, :format => :js) %>",
  dataType: 'script'
});

In your controller set the response content-type to text/plain:

respond_to do |format|
  format.js { render :content_type => "text/plain" } # needed for IE
end

The js response should now properly eval in all browsers!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top