头内容类型:应用程序/ JSON返回的text / html [英] header Content-Type: application/json returning text/html
问题描述
我有以下的code是应该输出的内容类型:JSON
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;
}
在我的生产服务器(GoDaddy的共享主机 - 阿帕奇2.4.10和放大器; PHP 31年5月4日)响应类型回来:应用程序/ JSON。但是我的本地WAMP服务器(Apache 2.4.9和放大器; PHP 5.5.12)的数据类型是text / html的'(如萤火虫看到)。这是造成我的问题,我制定了一系列需要是JSON API年代。我的第一个猜测是Apache的一个MIME类型的问题,但已经不适合我了平移。
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.
任何想法?我很抱歉,如果这也许更多的是服务器配置的问题。但我感到困惑,并认为这可能是我错过了我的php.ini的东西,甚至我的code。
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!
编辑 - 我与它摆弄了一下,学到东西。如果我改变了内容类型应用程序/ XML,我的生产服务器吐出来反映XML标准的格式不正确的内容预期的错误。然而,我的本地WAMP的服务器依然采用与内容类型text / html的响应头。有没有覆盖Apache或在PHP头分配设置?
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?
推荐答案
我仍然不知道为什么我的两个服务器都header命令的行为不同。但好消息是,我在Laravel这样做的,我发现了一个解决方案在那里。这精美的作品对我来说:
I still have no idea why my two servers are behaving differently with the header command. But the good news is that I am doing this within Laravel and I found a solution there. This works beautifully for me:
public static function getTest()
{
$result = ['foo' => 'bar'];
return Response::json($result);
}
这篇关于头内容类型:应用程序/ JSON返回的text / html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!