Phoenix渲染404和500为JSON [英] Phoenix Rendering 404 and 500 as JSON
问题描述
我在不使用--no-html
选项的情况下启动了一个phoenix项目,现在正在尝试确保404
和500
错误呈现为JSON.该项目从Phoenix 1.1.0
开始,现已更新为1.1.4
.
I started a phoenix project without using the --no-html
option, and am now trying to ensure that the 404
and 500
errors render as JSON. The project started on Phoenix 1.1.0
, and has been updated to 1.1.4
.
我已经将config/config.exs
文件的render_errors
(在config :my_app, MyApp.Endpoint
下)修改为[view: MyApp.ErrorView, format: "json", accepts: ~w(json)]
.
I've modified the config/config.exs
file's render_errors
(under config :my_app, MyApp.Endpoint
) to be [view: MyApp.ErrorView, format: "json", accepts: ~w(json)]
.
所有路由均接受JSON,目前均不接受HTML.
The routes all accept JSON and currently none of them accept HTML.
我已经修改了web/web.ex
文件以删除view
函数中的Phoenix.HTML使用,并且我已经修改了web/views/error_view.ex
来呈现JSON.
I've modified the web/web.ex
file to remove the use Phoenix.HTML in the view
function, and I've modified the web/views/error_view.ex
to render JSON.
但是,此时404
和500
错误仍然返回html.
However at this point both 404
and 500
errors still return html.
推荐答案
您是否更新了config.exs
?
config :my_app, MyApp.Endpoint,
# ...
render_errors: [accepts: ~w(html json)],
# ...
又返回哪种html?也许这是您的开发环境的phoenix调试页面,您可以在config/dev.exs
And what kind of html is returned? Maybe it is the phoenix debug page for your dev environment which you can disable in config/dev.exs
config :my_app, MyApp.Endpoint,
# ...
debug_errors: false,
# ...
这篇关于Phoenix渲染404和500为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!