简体   繁体   English

在脚本中设置 application/json 内容类型不起作用

[英]Setting application/json content-type in script doesn't work

I have a fairly simple php script on my site.我的网站上有一个相当简单的 php 脚本。 It basically greps a file for some text, then send it to the user as json.它基本上 greps 文件中的一些文本,然后将其作为 json 发送给用户。 On my local system, it works fine.在我的本地系统上,它工作正常。 On my Reclaim Hosting site, the content-type comes out text/html.在我的 Reclaim Hosting 网站上,内容类型为 text/html。 Here are the relevant lines:以下是相关行:

header('Content-Type: application/json');
$json_output = json_decode(implode("\n", $matches[0]));
print_r(json_encode($json_output));

I set my php.ini to:我将 php.ini 设置为:

output_buffering = Off

php 7.3.6. php 7.3.6。 Apache 2.4.46 Apache 2.4.46

I was messing around so much with the file that I'm not quite sure of the entire sequence, but the error log showed a header already sent, as @Barmar suggested.我对文件搞得太乱了,我不太确定整个序列,但错误日志显示 header 已经发送,正如@Barmar 建议的那样。 In the end the format was UTF-8 BOM and the BOM was doing it.最后的格式是 UTF-8 BOM 并且 BOM 正在这样做。

So the working output lines are now:所以工作 output 行现在是:

header('Content-Type: application/json');
echo implode("\n", $matches[0]);

Thanks for the help.谢谢您的帮助。

EDIT: To be clear a BOM in the script file triggers this.编辑:要清除脚本文件中的 BOM 会触发此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM