3

This is preventing me from using JSON and that prevents the usage of using React or Ember. When sending a request to the server using AJAX, I get an extra undefined unicode character which causes a parser error since it breaks JSON.

I have checked every php file I have for an extra throw or echo or print or any other function which would print something but there is nothing. This isn't limited to JSON, but when waiting for text, this wouldn't be a problem. But with JSON it would be parsed and it would break.

Is there anything that would be likely to interfere here?

For more clarification, take a look at this. The red dot represents an undefined character.

enter image description here

PS. Also, I'm not closing any PHP file with ?> tag and putting the start tag at the beginning of each file.

PPS. I'm using Sublime Text 3 and I don't believe it has anything to do with BOM. Also the red dot in chrome and firefox represents a non-printable special unicode character which is sent from server and there is only PHP on the server side.

MahdiPOnline
  • 329
  • 3
  • 12

1 Answers1

4

As answered in the link i posted in the comments above.

"Windows Notepad adds BOM information to UTF-8 files (i.e. FFFE or FEFF at the beginning of the file) which would explain this behavior. In Notepad++ for example you can prevent UTF-8 from including BOM information using the "Format" menu"


Make sure that included php files are also checked for BOM

Use this command for search and remove of BOM in files!:

find . -type f -exec sed '1s/^\xEF\xBB\xBF//' -i.bak {} \; -exec rm {}.bak \;

Community
  • 1
  • 1
DTH
  • 1,133
  • 3
  • 16
  • 36
  • Thanks for the answer. But I'm using Sublime Text 3 on Linux Ubuntu and by default it uses UTF-8 without BOM. – MahdiPOnline Sep 17 '15 at 05:41
  • 1
    @MahdiPOnline thats true. Did you, at any time, opened the file with another editor, which might have opened it as UTF-8 with BOM and therefore saved it with BOM, thus leading Sublime Text 3 to believe that it needs to save it with BOM in the future. Are you still seeing the same BOM character in your response ? – DTH Sep 17 '15 at 06:40
  • I saved all files again without BOM option in ST3 but still the error shows up. I'm sure that's not the problem. Is there anyway to see what echo is trying to output? – MahdiPOnline Sep 17 '15 at 09:12
  • 1
    @MahdiPOnline have you searched all files (assuming character is coming from some other file). You could look at this thread on how to search for BOM: http://stackoverflow.com/questions/204765/elegant-way-to-search-for-utf-8-files-with-bom – DTH Sep 17 '15 at 09:57
  • 1
    If you are including other php files in the php file which generates the response, and those files are not saved without BOM, they(it) could be the reason why you see BOM in your response ... look at this thread which had a similar problem: http://stackoverflow.com/questions/7512474/bom-randomly-appears-in-json-reply – DTH Sep 17 '15 at 10:12
  • Turns out a third party library had this problem. Thanks! This helped: http://stackoverflow.com/questions/204765/elegant-way-to-search-for-utf-8-files-with-bom – MahdiPOnline Sep 17 '15 at 10:21