奇怪的“406 不可接受"错误 [英] Weird "406 not acceptable" error

查看:48
本文介绍了奇怪的“406 不可接受"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试通过 Javascript 执行此操作时,出现 406 Not Acceptable 错误:

When I try to hit this action via Javascript, I get a 406 Not Acceptable error:

  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

这是来自 jQuery,但我在发送之前做的是正确的:

This is from jQuery, but I'm doing the right beforeSend stuff:

  $.ajaxSetup({ 
    beforeSend: function(xhr) {
      xhr.setRequestHeader("Accept", "text/javascript");
    },
    cache: false 
  });

这是我的请求标头:

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

推荐答案

我破案了!

我在 get 请求中发送了一个 format 参数,以便告诉服务器向我发送 Markdown 而不是 HTML.这是我的 Javascript:

I was sending a format parameter with my get request in order to tell the server to send me markdown instead of HTML. Here's my Javascript:

$.get("/annotations/" + annotation_id, {format: 'raw'}, function(data) {
});

然后我在 format.js 块中寻找这个参数:

and then I was looking for this parameter in the format.js block:

   format.js {
     if params[:format] == "raw"
       render :text => @annotation.body.to_s
     else
       render :text => @annotation.body.to_html
     end
   }

但显然 format 参数混淆了 respond_to 块.我将它从 {format: 'raw'} 更改为 {markdown: 'true'} 并且它有效.

but apparently a format parameter confuses the respond_to block. I changed it from {format: 'raw'} to {markdown: 'true'} and it works.

我猜这是 Rails 中的一个错误?

I guess this is a bug in Rails?

这篇关于奇怪的“406 不可接受"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆