【问题标题】:jquery $.ajax not working in firefox against rails (406 response) (works in chrome & IE)jquery $.ajax 不能在 firefox 中对 rails 工作(406 响应)(适用于 chrome 和 IE)
【发布时间】:2010-12-22 00:26:39
【问题描述】:

我有一个 Rails 后端,正在测试下面的 jquery 代码:

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

此代码在 Chrome 和 IE 中运行良好。但是在 Firefox(使用 Firebug)中,我看到了这个: http://localhost:3000/notifications/detect_type.json 406 不可接受

这是日志中的一个 Firefox 请求:

处理 NotificationsController#detect_type (127.0.0.1 2010-12-21 17:05:59) [POST] 参数: {"action"=>"detect_type", “内容”=>“226 南艾默生丹佛 公司 80209", “控制器”=>“通知”} 用户 列 (2.0ms) 显示字段来自 users 用户负载 (37.4ms) 选择 * FROM users WHERE (users.id = '1') LIMIT 1 在 58 毫秒内完成(查看: 1, DB: 40) | 406 不可接受 [http://localhost/notifications/detect_type.json]


这是日志中的 chrome 请求:

处理 NotificationsController#detect_type (127.0.0.1 2010-12-21 17:06:41) [POST] 参数:{"action"=>"detect_type", “内容”=>“226 南艾默生 80209”, “控制器”=>“通知”}
用户列(2.1ms)显示来自users的字段
用户负载 (30.4ms)
SELECT * FROM users WHERE (users.id = '1') LIMIT 1 完成 在 100 毫秒内(查看:1,DB:33)|
200 OK [http://localhost/notifications/detect_type.json]

我被难住了。想法?

【问题讨论】:

  • 对于任何可能遇到此问题的人,我在 firefox 中遇到了完全相同的错误,然后我意识到我在 firefox 中禁用了 javascript。

标签: jquery ruby-on-rails


【解决方案1】:

奇怪的是,解决方案是在 Rails 端执行此操作:

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

JQuery 错误?不确定...

【讨论】:

【解决方案2】:

根据快速搜索,可能 406 表示浏览器(在本例中为 Firefox)拒绝接受从服务器为请求传递的内容类型。 (This is one such explanation.)

尝试将 Firefox 配置为接受 json。基于this post,Firefox 可能想要使用扩展...

更新

由于这似乎是对 $.ajax 的直接使用,您应该能够在不更改任何奇怪的 Firefox 客户端设置的情况下使其工作。尝试通过在初始化期间添加一个选项来明确告诉 jquery 返回的数据类型应该是什么,如下所示:

数据类型:“json”

有关更多信息,请参阅相关的 jquery 文档 here

【讨论】:

  • 我认为这个答案在正确的轨道上......但我想让这个响应只适用于任何火狐......我是否必须切换到 XML?
  • 你不应该这样做;我可以使用带有 $.ajax 的 Firefox - 尝试添加 dataType: "json";我将添加到我的答案中
  • dataType: "json" 是我尝试的第一件事。仍然得到 406。
猜你喜欢
  • 2013-04-22
  • 1970-01-01
  • 2011-10-15
  • 2012-02-14
  • 2014-05-24
  • 2016-01-29
  • 1970-01-01
  • 2021-01-07
  • 1970-01-01
相关资源
最近更新 更多