重定向期间使用 PHP7 的 AJAX 出现错误 500

问题描述 投票:0回答:1

我将网站从 php5.5 切换到 php7,现在有一个非常奇怪的错误:当我执行包含重定向的特定 ajax 请求时,服务器会返回 http 500 错误代码,同时返回良好的 html 内容(我可以从 Chrome 控制台看到它)。

当我在 php5 中执行完全相同的请求时,我没有更多错误。当我在没有 ajax 的新选项卡中在 php7 中执行完全相同的请求时,我没有更多错误。奇怪的是,当我有时在代码中添加 var_dump 时,我没有更多错误。

当我收到 500 错误代码时,我的日志中没有任何内容,并且 html 内容中也没有显示任何错误。

这是有错误的响应标头:

HTTP/1.1 500 Internal Server Error
Date: Mon, 03 Apr 2017 10:44:20 GMT
Server: Apache
Expires: Tue, 04 Apr 2017 10:44:20 GMT
Accept-Ranges: bytes
Set-Cookie: PHPSESSID=25e73849544a66f7512533246cde4d21; path=/
Last-Modified: Mon, 03 Apr 2017 10:44:20 GMT
Content-Length: 11718
Connection: close
Content-Type: text/html; charset=utf-8

并且没有错误(在我添加 var_dump 之后):

HTTP/1.1 200 OK
Date: Mon, 03 Apr 2017 10:45:44 GMT
Server: Apache
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 3223
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

您知道可能出了什么问题吗?

ajax php-7 http-status-code-500
1个回答
0
投票

我发现了问题,我使用的代码中的某个地方有一个自定义异常,默认情况下启动了一个带有 500 http 错误的标头,但是在标头被异常启动后脚本并没有被杀死,所以很难找到它:

  public function __construct($message = "Critical error", $code = 500, Exception $previous = Null) {
    parent::__construct($message, $code, $previous);
    if($this->getHttpHeader())
      header($this->getHttpHeader());
  }

  public function getHttpHeader() {
   $header = get_header_for_code($this->getCode());
   $this->http_header = $header;
   return $this->http_header;
  }

但我还是不知道为什么这个问题只出现在php7中并且只在某些情况下出现,而且我也没有时间去查找。

© www.soinside.com 2019 - 2024. All rights reserved.