头信息设置json还是html,php - header Content-Type: application/json returning text/html - Stack Overflow...

博主在本地WAMP服务器和生产环境(Godaddy)上遇到一个PHP代码问题,代码设置为输出JSON内容类型,但本地环境显示为'text/html',而生产环境正确显示为'application/json'。当尝试将内容类型改为'application/xml'时,生产环境给出预期错误,而本地环境仍保持'text/html'。博主怀疑可能是Apache或PHP配置问题,寻求解决方案。
摘要由CSDN通过智能技术生成

I have the following code that is supposed to output in the content-type: json.

public static function getTest()

{

header('Content-Type: application/json; charset=utf-8');

$result = ['foo' => 'bar'];

$json = json_encode($result);

echo $json;

}

On my production server (godaddy shared hosting - Apache 2.4.10 & PHP 5.4.31) the response type comes back: application/json. However on my local WAMP server (Apache 2.4.9 & PHP 5.5.12) the response type is 'text/html' (as seen in Firebug). This is causing me problems as I develop a series of API's that need to be json. My first guess was an apache mime type issue but that has not panned out for me.

Any ideas? I am sorry if this is perhaps more of a server config issue. But I am baffled and thought it possible that I missed something in my PHP.ini or even my code.

All help gratefully received. Thanks!

EDIT - I have fiddled with it a bit and learned something else. If I change the 'Content-type' to application/xml, my production server spits out the expected error reflecting the improperly formatted content for the xml standard. However, my local WAMP server still uses a response header with a content-type of text/html. Is there a setting to override header assignments in Apache or in PHP?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值