0

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,

mentinet
  • 744
  • 3
  • 9
  • 23
  • Do you have a link? Because that's the easiest way to verify if a BOM is the issue. – Repox Jul 31 '15 at 15:05
  • My environment is local only. I have saved the returned JSON in a file and put it there: http://www.mentinet.fr//so/test.json Thanks for your help. – mentinet Jul 31 '15 at 15:31
  • I downloaded the file and opened it in a hex editor; the first three hex values are `\xEF\xBB\xBF` which is the UTF-8 BOM signature. The BOM signature is probably set by your editor, where you also should disable it. – Repox Aug 01 '15 at 11:59
  • I don't have the choice between UTF-8 with or without BOM in my editor, just UTF-8. And how would that explain that the exact same files (just unzipped) work on my client's environment and not on mine? – mentinet Aug 06 '15 at 08:40
  • What editor are you using? How is your client working with? What differs between your environment and your clients environment, etc. We have too little information. I can only conclude that your file has the UTF-8 BOM. – Repox Aug 07 '15 at 09:39
  • The JSON data are generated by a ZF2 method called via Ajax, from a JavaScript file loaded in the view. My client and I made the test to deploy the exact same files (from a single zip file) on our environment and I have the bug, he doesn't. I am therefore assuming the issue comes from our environments configurations. As I can't give all the details of both environments (which are apache 2.2.3 and PHP 5.4 on Suse server for sure), I am wondering if you would have any hint of where to look at in the server/apache/php configurations, for some encoding settings. Thank you – mentinet Aug 10 '15 at 12:38

1 Answers1

0

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

Community
  • 1
  • 1
mentinet
  • 744
  • 3
  • 9
  • 23