【问题标题】:My custom HTTP error message is getting overwritten in php我的自定义 HTTP 错误消息在 php 中被覆盖
【发布时间】:2012-02-13 00:42:34
【问题描述】:

所以我使用 cakephp 并使用 ajax 提交表单。在某些情况下,它会返回自定义错误。

例如,在某一时刻它可能是这样的:

Failed to load resource: the server responded with a status of 412 
([{"field":"PaymentCardholderName","message":"Please enter CardHolder's Name."},
{"field":"PaymentCardNumber","message":"Please enter Card Number."},
{"field":"PaymentCvvNumber","message":"Please enter CVV Number."},
{"field":"PaymentBillingAddress","message":"Please enter your Billing Address"}])

这是服务器逻辑以防万一有人需要它

header('HTTP/1.1 412 ' . json_encode($error));

当我在本地打开它时,我会看到我的自定义错误消息。当我将它部署到远程服务器时,我只是看到我的自定义错误消息被覆盖了

Failed to load resource: the server responded with a status of 412 
(Precondition Failed)

应该是一些配置,但是我没找到。

【问题讨论】:

  • 为什么地球你会把这样的数据放在响应行中?将它放在响应的正文中 (echo it) - HTTP 不是旨在以您尝试使用它的方式工作。
  • @DaveRandom 感谢您的回复。我正在尝试触发 jquery ajax 错误。你能帮我吗?在这种情况下如何发送错误
  • 请将发出请求的 jQ 代码添加到您的问题中,以便我们为您提供正确的代码

标签: php apache cakephp apache2 webserver


【解决方案1】:

另外,而不是假定HTTP/1.1

这样做:

header($_SERVER["SERVER_PROTOCOL"]." 412 Precondition Failed");

而不是这个:

header("HTTP/1.1 412 Precondition Failed");
header("HTTP/1.0 412 Precondition Failed");

为什么?因为$_SERVER["SERVER_PROTOCOL"] 会根据您的浏览器为您提供HTTP/1.1HTTP/1.0。如果你用错了,apache 可能会在响应中添加意想不到的内容。例如。开头的 4 位十六进制代码(校验和?),尾随零。

【讨论】:

  • 最重要的是,永远不应该依赖状态行。它仅用作人类可读的字符串。代码应该只看代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-18
  • 2012-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多