Web服务器响应,而生成的UTF-8(BOM)JSON服务器、Web、UTF、JSON

2023-09-10 21:54:41 作者:伱是涐旳緈諨╮

我有使用它返回一个JSON格式阵列的方法的ZF2应用:

I have a ZF2 application with a method which returns a JSON formatted array using:

$response->setContent(json_encode($reponse));
return $response;

该请求被使用jQuery 1.10.2通过Ajax发送,当我拦截响应体,使用开发模式或小提琴手,我可以看到的 http://jsonlint.com/ 的JSON是无效的。这样一来,我的阿贾克斯成功回调触发与IE8但更多的新版本或浏览器如Firefox或Chrome,它直接转到错误回调。

The request is sent via Ajax using jQuery 1.10.2 and when I intercept the response body, using the developer mode or Fiddler, I can see in http://jsonlint.com/ that the JSON is not valid. As a result, my Ajax success callback is triggered with IE8 but with more recent versions or browsers such as Firefox or Chrome, it directly goes to the error callback.

这似乎是JSON的是由第一大括号无效,当我重新输入,好像还行。因此,我怀疑有些BOM字符的文件的开头。

It seems like the invalidity of the JSON is caused by the first curly brackets, when I retype it, it seems okay. I therefore suspect some BOM characters at the beginning of the file.

什么是更奇怪的是,具有完全相同的源$ C ​​$ C,我的客户有任何问题都与Internet Explorer 10在他的环境。

What is even odder is that, with the exact same source code, my client has no issue at all with Internet Explorer 10 in his environment.

请问你们有什么可能会导致这样的差异在我们的PHP / Apache的环境中的任何想法?

Would you guys have any idea of what could cause such a difference in our PHP/Apache environments?

感谢你的帮助。 问候,

Thank you for your help. Regards,

推荐答案

该问题是由一些BOM字符,从而出现了一些文件的确引起的。

The problem was indeed caused by some BOM characters which appeared in some files.

我的 config.Global.conf 文件是EN codeD的UTF8(带BOM),再加上它有这个开头&LT ; FEFF>< FEFF> ,我可以看到VIM打开时。

My config.Global.conf file was encoded in UTF8 (with BOM), plus it had this at the beginning <feff><feff> that I could see when opening it with VIM.

我从我的配置文件中删除这些额外的BOM字符,加上转换UTF8(带BOM)文件UTF8 W / O型BOM固定的问题。

I fixed the issue by removing these extra BOM characters from my configuration file, plus converting the UTF8 (with BOM) files in UTF8 w/o BOM.

选中这里看我怎么发现哪些文件被导致此问题:的查找BOM来源Zend框架2

Check here to see how I found out which files were causing the issue: Find source of BOM in Zend Framework 2