【发布时间】:2015-06-24 12:51:53
【问题描述】:
我正在尝试返回一个 ut8_encoded 的 json。我在它之前设置了 HTTP 响应代码的标头。
/**
* Set the response header and HTTP response code and response the data
* @param $data array of response to be encoded as json
* @param int $status the http status (200 is default)
* @return string the json presentation of the array given
*/
public function response($data, $status = 200)
{
header("HTTP/1.1 " . $status . " " . $this->getRequestStatus($status));
echo utf8_encode(json_encode($data, JSON_UNESCAPED_SLASHES));
}
结果正常,但有警告 -
警告:无法修改标头信息 - 标头已由第 113 行的 RestApi.php 中的(从 RestApi.php:115 开始的输出)发送的标头
编辑: 抱歉,我可能应该添加它 - 警告正在谈论导致警告的那两行。
如何删除警告?
如果我将此部分添加到 .htaccess 中,可能会发生错误?
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "*"
Header set Content-Type "application/json; charset=utf-8"
</IfModule>
我才意识到。所以也许我真的不需要对响应进行编码
【问题讨论】:
-
在php文件顶部使用ob_start()
-
看起来您在致电
response()之前已经向浏览器发送了一些内容。向我们展示调用响应的行 -
你
utf8_encode一个JSON结果是为了什么?!
标签: php utf-8 http-headers