Weird “406 not acceptable” error

前端 未结 12 980
南方客
南方客 2020-11-27 05:38

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])
         


        
相关标签:
12条回答
  • 2020-11-27 05:55

    I hit this problem when I forgot to add :remote => true to my Ajax form.

    0 讨论(0)
  • 2020-11-27 05:56

    I cracked the case!

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

    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
       }
    

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

    I guess this is a bug in Rails?

    0 讨论(0)
  • 2020-11-27 05:59

    Can you try without setting the Accept Header? It should actually work even without the Accept header.

    0 讨论(0)
  • 2020-11-27 06:05

    This happened to me when using HTTPRiot connecting to a JSON rendering web app from an iPhone app. It appears that the issue is due to Rails expecting an Accept HTTP header that it is well, comfortable with. As such, I used Firefox's LiveHTTPHeaders extension to see what headers work without a 406. In any case the Accept string that worked was:

    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.

    0 讨论(0)
  • 2020-11-27 06:06

    If you are using jRails this was causing a lot of problems for me, here is my application.js file:

    $(document).ready(function () {
        $.ajaxSetup({
            beforeSend: function (xhr) {
                xhr.setRequestHeader("Accept", "text/javascript, text/html, application/xml, text/xml, */*");
            }
        });
    });
    
    0 讨论(0)
  • 2020-11-27 06:06

    i was calling the url for js format but had this in the controller:

    respond_to do |format|
      format.html 
    end
    

    this worked fine in Safari, but not with Firefox.

    naturally i was missing something, should be:

    respond_to do |format|
      format.html
      format.js
    end
    

    and now both browsers are fine.

    0 讨论(0)
提交回复
热议问题