Ruby-on-rails 奇怪的“406 不可接受”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1414446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Weird "406 not acceptable" error
提问by Tom Lehman
When I try to hit this action via Javascript, I get a 406 Not Acceptableerror:
当我尝试通过 Javascript 执行此操作时,出现406 Not Acceptable错误:
def show
@annotation = Annotation.find_by_id(params[:id])
respond_to do |format|
format.html {
if @annotation.blank?
redirect_to root_path
else
redirect_to inline_annotation_path(@annotation)
end
}
format.js {
if params[:format] == "raw"
render :text => @annotation.body.to_s
else
render :text => @annotation.body.to_html
end
}
end
end
This is from jQuery, but I'm doing the right beforeSend stuff:
这是来自 jQuery,但我正在做正确的 beforeSend 东西:
$.ajaxSetup({
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "text/javascript");
},
cache: false
});
Here are my request headers:
这是我的请求标头:
Host localhost:3000
User-Agent Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
Accept text/javascript
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
Connection keep-alive
X-Requested-With XMLHttpRequest
Content-Type application/x-www-form-urlencoded
采纳答案by Tom Lehman
I cracked the case!
我破案了!
I was sending a formatparameter with my get request in order to tell the server to send me markdown instead of HTML. Here's my Javascript:
我在formatget 请求中发送了一个参数,以便告诉服务器向我发送 Markdown 而不是 HTML。这是我的Javascript:
$.get("/annotations/" + annotation_id, {format: 'raw'}, function(data) {
});
and then I was looking for this parameter in the format.jsblock:
然后我在format.js块中寻找这个参数:
format.js {
if params[:format] == "raw"
render :text => @annotation.body.to_s
else
render :text => @annotation.body.to_html
end
}
but apparently a formatparameter confuses the respond_toblock. I changed it from {format: 'raw'}to {markdown: 'true'}and it works.
但显然一个format参数混淆了respond_to块。我把它从{format: 'raw'}改为{markdown: 'true'}并且它有效。
I guess this is a bug in Rails?
我想这是Rails中的一个错误?
回答by Dustin
include "format.js" in your respond_to block
在您的 respond_to 块中包含“format.js”
回答by yuvalz
This happened to me when using HTTPRiotconnecting to a JSON rendering web app from an iPhone app. It appears that the issue is due to Rails expecting an AcceptHTTP header that it is well, comfortable with. As such, I used Firefox's LiveHTTPHeadersextension to see what headers work without a 406. In any case the Accept string that worked was:
我在使用HTTPRiot从 iPhone 应用程序连接到 JSON 渲染 Web 应用程序时发生了这种情况。看来这个问题是由于 Rails 期待一个Accept它很好、很舒服的HTTP 标头。因此,我使用 Firefox 的LiveHTTPHeaders扩展来查看没有 406 的标头是否有效。无论如何,有效的 Accept 字符串是:
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Another area that I would examine is the JSON-producing controller. If the controller is missing a format directive to specify it can return JSON in response, that too may cause a 406 error.
我要检查的另一个领域是 JSON 生成控制器。如果控制器缺少格式指令来指定它可以返回 JSON 作为响应,这也可能导致 406 错误。
回答by Sergey
Check your application.js for require jquery_ujs
检查您的 application.js 是否需要 jquery_ujs
//= require jquery_ujs
回答by Rishav Rastogi
Can you try without setting the Accept Header? It should actually work even without the Accept header.
您可以尝试不设置接受标题吗?即使没有 Accept 标头,它实际上也应该可以工作。
回答by noroot
Just use this code in controller action method format block:
只需在控制器操作方法格式块中使用此代码:
format.js { render :nothing => true }
回答by Guy Argo
I hit this problem when I forgot to add :remote => trueto my Ajax form.
当我忘记添加:remote => true到我的 Ajax 表单时,我遇到了这个问题。
回答by kitschmaster
i was calling the url for js format but had this in the controller:
我正在调用 js 格式的 url,但在控制器中有这个:
respond_to do |format|
format.html
end
this worked fine in Safari, but not with Firefox.
这在 Safari 中运行良好,但不适用于 Firefox。
naturally i was missing something, should be:
自然我错过了一些东西,应该是:
respond_to do |format|
format.html
format.js
end
and now both browsers are fine.
现在两个浏览器都很好。
回答by Darren
For me it was a simple before_filter that was restricting a action that renders a js file, once I added the :except => [:action] to the before_filter block it was fine.
对我来说,这是一个简单的 before_filter,它限制了呈现 js 文件的操作,一旦我将 :except => [:action] 添加到 before_filter 块中,它就可以了。
回答by David Andres
Is this served through Apache? You may want to take a look at http://forums.alwayswebhosting.com/showthread.php?p=8381, which describes scenarios where security policy interferes with requests.
这是通过 Apache 提供的吗?您可能需要查看http://forums.alwayswebhosting.com/showthread.php?p=8381,它描述了安全策略干扰请求的场景。
EDIT: The URL referenced above advocates turning off request-sniffing security policy across an entire site, which makes the site vulnerable. Setting the SecFilterEngine option to Off in the .htaccess, which is what is prescribed in the URL, should be done only to zero in on the source of the problem. It should not be considered a long term solution.
编辑:上面引用的 URL 提倡关闭整个站点的请求嗅探安全策略,这使站点容易受到攻击。在 .htaccess 中将 SecFilterEngine 选项设置为 Off,这是 URL 中规定的,应该只在问题根源上将其设置为零。不应将其视为长期解决方案。

