views:

77

answers:

2

Hi,

I have a form that i want submitting with AJAX, using rails 3 and jquery. The problem that I am facing is that rails is responding to the AJAX request as HTML too. After a little search I found that this has something to do with the correct Accept headers not being passed. How do i fix this?

This is the controller code

  respond_to do |format|
    format.js { render 'user/create' }
    format.html { redirect_to ((params[:feed][:url].nil?)?url_for(:home) : params[:feed][:url]) }
  end

It seems to work on a friends firefox, and on my chrome too, smthng wrong with my firefox?

UPDATE: It seems that the error arises only when I use a proxy service as JonDo, which probably changes the accept headers... Is there a way to force rails to use js format if the X-requested-by header is present?

Thanks

A: 

Your controller code looks correct. Are you sure that you have added the .js suffix to the AJAX URL in your form? That's how the responder knows what format you want for the response. The default format is HTML so omitting the suffix would look like it's responding to a HTML request.

bjg
adding .js will not let the page degrade to HTML, when js is off in the browser.
Amit
@Amit I don't understand what you mean here. If Javascript is off in the browser then the AJAX call will not be made so the `.js` respond block will not be called. The `.js` suffix for the Javascript GET request, nothing else
bjg
but will the .html respond block respond?
Amit
No, not if the `.js` suffix is provided in the request URL
bjg
A: 

You likely didn't set the dataType properly. See the docs (this is &.post) If you set dataType to "js" you'll be all good!

brad
the dataType is set to "script". I used the jquery.rails.js data handler, so that is already being set by it.
Amit
right sorry, forgot 'script' is the dataType you're looking for. Can't comment much further then, I've never used the rails 3 jQuery lib.
brad
np :)I did a forced if request.xhr? and then rendered a file...
Amit