jlfenaux
jlfenaux

Reputation: 3291

406 Not Acceptable error when using Jquery $.post in Firefox but not in Safari or Chrome

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

Upvotes: 4

Views: 8431

Answers (5)

korinthe
korinthe

Reputation: 288

None of the above suggestions (atmorell, Paul Groves) fixed it for me... then I found jquery $.ajax not working in firefox against rails (406 response) (works in chrome & IE)

As I commented there, the accepted answer worked! (using format.js rather than format.json, and "render :text => item.to_json")

For the record, I'm not making a straight .ajax call; rather I'm using JQuery.UI's autocompleter:

$(document).ready(function() {
    $("input#task_summary_task_wbs").autocomplete({
        source: "/tasks/summary.js"
    });
});

and this in my TasksController:

# GET /tasks/summary.js
def summary
  @tasks = Task.find(:all,
                     :conditions => ['is_summary = true AND wbs like ?',
                                     "%#{params[:term]}%"],
                     :order => :wbs)
  respond_to do |format|
    format.js {
      render :text => @tasks.collect {|t| t.wbs_name}.to_json
    }
  end
end

a la Railscast #102.

Upvotes: -1

atmorell
atmorell

Reputation: 3068

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

Upvotes: 4

Paul Groves
Paul Groves

Reputation: 4051

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")} 
})

Upvotes: 4

StefanS
StefanS

Reputation: 876

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/

Upvotes: 0

kgiannakakis
kgiannakakis

Reputation: 104188

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

Upvotes: 0

Related Questions