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])
I hit this problem when I forgot to add :remote => true
to my Ajax form.
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?
Can you try without setting the Accept Header? It should actually work even without the Accept header.
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.
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, */*");
}
});
});
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.