Web server response generates UTF-8 (BOM) JSON

僤鯓⒐⒋嵵緔 提交于 2019-12-20 05:55:05

问题


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

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

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.

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.

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.

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

Thank you for your help. Regards,


回答1:


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

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.

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.

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



来源:https://stackoverflow.com/questions/31749384/web-server-response-generates-utf-8-bom-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!