Rails 3:尝试在浏览器中查看 json 时出现 HTTP 406 和空白页面
我对 Rails 和 ajax 有点陌生,在尝试在控制器中调试一些相当简单的 ajax 请求时,我遇到了以下挫败感。
我有以下代码:
respond_to do |format|
#format.html { render :json => db_clean_response.to_json }
format.json { render :json => db_clean_response.to_json }
end
当我尝试通过手动输入 URL 和参数来调试此问题时,控制器正确准备响应,但 Rails 会以 HTTP/406 Not Acceptable
进行响应,除非我取消注释 format.html
命令。 (请注意,format.html
内容只是为了尝试解决此问题。)
我不希望此函数以 html 响应,因为它仅适用于 ajax 内容。此外,Rails 响应将 json 丑陋地打印到空的 html 上,我想使用像 JSONView 这样的插件来美化它。
这是怎么回事?我觉得想要的结果很简单,但是有些东西、某个地方把它搞砸了。当我尝试调试 ajax 时,我希望我的浏览器能够在不蹩脚的情况下拉出该死的 json :-(
I am a little new with Rails and ajax, and I'm encountering the following frustration whilst trying to debug some fairly simple ajax requests in my controllers.
I have the following code:
respond_to do |format|
#format.html { render :json => db_clean_response.to_json }
format.json { render :json => db_clean_response.to_json }
end
When I try to debug this by manually entering the URL and parameters, the controller correctly prepares the response, but Rails responds with HTTP/406 Not Acceptable
unless I uncomment the format.html
command. (Note that the format.html
stuff is just for trying to fix this.)
I don't want this function responding with html, as its only for ajax stuff. Besides, Rails responds with the json uglyprinted onto empty html, and I'd like to use a plugin like JSONView to prettify it.
What's going on here? I feel like the desired result is very simple, but something, somewhere is messing it up. When I try to debug ajax I want my browser to pull up the damn json without being lame :-(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,将
format=json
添加到 URL 参数可以实现我想要的效果:强制 Rails 返回带有所有正确标头的 jsonIt turns out that adding
format=json
to the URL parameters does what I want: forces Rails to return json with all the right headers