Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
JSON response - browser trying to save
2 Posts
JSON response - browser trying to save

I have a view that uploads logos on a client's website. It uses Ajax (JQuery) to upload the image. In return the controller renders render :json => {:url..... etc. so that the logo gets updated on the client side without a page reload.

The problem I'm having is that pretty much all browsers except for chrome beta 5 will open a dialog to save or open that application/json response.

I have tried to research this, but it doesn't seem like it is known issue does the rails expert here have an idea of how to get rid of this dialog maybe suppress it somehow. FYI the image uploads correctly and because when I refresh the page the new image shows. And as mentioned above in chrome beta 5.x.x it works exactly as it should with automated reload of image on the client side.

Any help would be appreciated.

Thanks

BC

It sounds like you are asking the controller to respond with a JSON response, but the requestor (browser) is not expecting it.

You have a few options here. The most obvious is to respond with an HTML result (like a web page) displaying a "Logo updated!" sort of message. But that's not using AJAX.

Using AJAX, you should have a form_to_remote or link_to_remote or some such that kicks off the process. If you do, it might look something like this:

 logos_path(:format => 'json'), :method => :put%>

Now, the controller is going to respond with a JSON object. To parse this object you (probably) need to use JavaScript, so you need to define a callback method to send the JSON object to. The callback JS might look like:


Now you just need a way to call that callback method. You can do this from the controller, by modifying your render :json to include a :callback option. It might look like:

render :json => logo.to_json, :callback => "parse_json_callback"

That should do the trick. If it's still not working, make sure you have this line in your layout:




...which is what pulls in the various JavaScript dependencies that make all this AJAX stuff work.

Hope this is helpful.

2 Posts
Login to add your message