gpt4 book ai didi

json - 为什么 JSON 类型的 HTTP 501 错误以 text/html 而不是 application/json 的形式呈现给 UI?

转载 作者:行者123 更新时间:2023-11-28 23:25:51 26 4
gpt4 key购买 nike

我在 Apache 上运行前端 Angular,在 Tomcat 上运行后端 Spring REST API。

我想在某些错误情况下抛出 500 错误,并希望将该响应作为 Json 呈现给 UI(Content-Type 为 application/json)。正如预期的那样,这在我的本地工作正常,Angular 能够解释 Json 响应,但是当涉及到一些常见的 QA 环境时,它失败了,因为 501 错误以 Angular 无法消化的文本/html 形式呈现给 UI。

我的本​​地和 QA 之间的区别是,Angular 在节点上运行,而 QA 使用 Apache。正在将带有 501 http 代码的 json 响应转换为 html 类型的 apache 吗?

我在浏览器上得到的响应如下。

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Not Implemented</title>
</head><body>
<h1>Internal Server error</h1>
<p>GET to /test/customer/getCustomer not supported.<br />
</p>
</body></html>

最佳答案

最后我找到了导致 Apache 将所有非 2XX 响应替换为它自己的错误文档的原因。您需要做的就是注释或触发 ProxyErrorOverride。有了这个 Apache 将代理传递错误响应,就像从后端服务器到 UI 一样,而不会丢失内容,例如它发送 Json 类型的错误响应,因为它不会丢失 header 。

ProxyErrorOverride 关闭

希望这对某人有帮助。

关于json - 为什么 JSON 类型的 HTTP 501 错误以 text/html 而不是 application/json 的形式呈现给 UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37200421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com