views:

606

answers:

4

I'm using Jquery (1.3.2) $.post command to trigger an ajax call to a rails server.

The code works great on Safari and on Google Chrome (mac), but when I tried it on Firefox (3.5.7), I got a weird '406 Not Acceptable' error.

When I look at the headers, it Firefox indicated that it accepted only ' text/javascript' responses. And the response Content-Type was 'text/html; charset=utf-8'.

In Chrome the accepted types were 'application/json, text/javascript, /, text/javascript' and the response Content-Type was 'application/json; charset=utf-8'.

I tried to force the content type in rails to 'text/javascript'

format.json do
   render :json => @races.to_json, :content_type => 'text/javascript'
end

The content type is indeed changed in Chrome, but not in Firefox where it remains 'text/html'.

Here is the code I used to trigger the ajax call.

$.post(
    "/locator",
    params, 
    function(data){...},
    "json"
);

Is there something I can do to make this work in Firefox? Thanks

A: 

It doesn't make sense that different browsers see different HTTP headers. Perhaps this is a caching issue.

kgiannakakis
@Kgiannakakis please suggest me a solution for http://stackoverflow.com/questions/2242418/inheriting-baseclass-to-an-ajax-enabled-wcf-service-class and http://stackoverflow.com/questions/2242788/ajax-enabled-wcf-service-doesnt-seem-to-work-for-me
Pandiya Chendur
A: 

I'm using the contentType option during the $.ajaxSetup call to set the content type explicitly. That work's across browsers: http://api.jquery.com/jQuery.ajax/

StefanS
+1  A: 

Add a .json extension to your URL in the post call

$.post(
"/locator.json"
...

Or (possibly better) add the following to your application.js to set headers for all ajax requests

jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} 
})
Paul Groves
Thanks Paul.The jQuery.ajaxSetup didn't provide any help, but I followed your first reply. I created a specific route in rails dedicated to the ajax callajaxlocator.json and it now works fine !Thanks.
jlfenaux
A: 

Had the same problem. It seems to be a firefox issue.

This works:

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

Credit goes to this blog.

Best regards. Asbjørn Morell

atmorell