BOM randomly appears in JSON reply

后端 未结 1 533
走了就别回头了
走了就别回头了 2021-01-14 03:16

I\'m implementing communication between two servers using JSON and cURL. The problem is, that sometimes there\'s BOM (byte order mark), appended before opening bracket in JS

相关标签:
1条回答
  • 2021-01-14 03:56

    I had the same problem. I was outputting json from PHP and there were other class files included at the top of the page. These files output nothing, but when they were included I was getting as many Byte Order Marks as I had included files. So if I had 4 includes, I also had 4 BOMs at the start of my json.

    I made sure the includes were not printing any data and there were no stray carriage returns outside the PHP tags. I tried headers such as "application-json", etc., but nothing worked.

    In the end, I simply opened each PHP file in notepad++, went to "Encoding" and changed it from UTF-8 to ANSI, then saved. That was all it took to get it working and returning valid json. I made no code changes to the PHP at all.

    This solution still feels less than ideal. Since we are not outputting anything from those included files there shouldn't be anything affected.

    0 讨论(0)
提交回复
热议问题