phil swenson
phil swenson

Reputation: 8894

jquery $.ajax not working in firefox against rails (406 response) (works in chrome & IE)

I have a rails backend and am testing the following jquery code against it:

  var content = $("#notification_content").val();
  var data = new Object();
  data.content = content;
  $.ajax({ url: "/notifications/detect_type.json", type:"POST", data: data, success: function(result  ){updateTypeDropDown(result)}});

This code works fine in Chrome and IE. However in Firefox (using Firebug), I see this: http://localhost:3000/notifications/detect_type.json 406 Not Acceptable

here is a firefox request in the log:

Processing NotificationsController#detect_type (for 127.0.0.1 at 2010-12-21 17:05:59) [POST] Parameters: {"action"=>"detect_type", "content"=>"226 south emerson denver co 80209", "controller"=>"notifications"} User Columns (2.0ms) SHOW FIELDS FROM users User Load (37.4ms) SELECT * FROM users WHERE (users.id = '1') LIMIT 1 Completed in 58ms (View: 1, DB: 40) | 406 Not Acceptable [http://localhost/notifications/detect_type.json]


here is a chrome request in the log:

Processing NotificationsController#detect_type (for 127.0.0.1 at 2010-12-21 17:06:41) [POST] Parameters: {"action"=>"detect_type", "content"=>"226 south emerson 80209", "controller"=>"notifications"}
User Columns (2.1ms) SHOW FIELDS FROM users
User Load (30.4ms)
SELECT * FROM users WHERE (users.id = '1') LIMIT 1 Completed in 100ms (View: 1, DB: 33) |
200 OK [http://localhost/notifications/detect_type.json]

I'm stumped. Ideas?

Upvotes: 1

Views: 1490

Answers (2)

phil swenson
phil swenson

Reputation: 8894

Oddly enough, the solution was to do this on the rails side:

format.js {
  render :text => type.to_json
}
format.json {
  render :json => type.to_json
}

JQuery bug? Not sure...

Upvotes: 2

Tahbaza
Tahbaza

Reputation: 9546

Based on a quick search it looks like perhaps 406 indicates a refusal by the browser (Firefox in this case) to accept the type of content being delivered from the server for the request. (This is one such explanation.)

Try configuring Firefox to accept json. Based on this post it looks like Firefox may want to use an extension...

UPDATE

Since this seems to be a straight use of $.ajax you should be able to get this to work without any odd Firefox client setting changes. Try explicitly telling jquery what the type of data returned should be, by adding an option during init, like so:

dataType: "json"

See the relevant jquery docs here for more info.

Upvotes: 0

Related Questions