Invalid JSON in Chrome, no problem in Firefox (so strange!)

前端 未结 3 1807
傲寒
傲寒 2020-12-06 18:19

All keys were double quoted. The whole element was an object. Firefox runs it well but Chrome reports \"invalid JSON\". Why?

This is full code.

3条回答
  •  囚心锁ツ
    2020-12-06 18:47

    You have a Unicode Byte Order Mark at the beginning of your PHP file. Because of this, and because it's before the opening , it gets sent to the client at the beginning of your JSON. This will make your JSON invalid, as those characters shouldn't appear at the beginning of the JSON data. Some browsers cope with it fine; other browsers, such as Chrome, are fussier and complain.

    Removing the Byte Order Mark by saving the file without that option set in your editor (how to do this is editor-dependent) will solve your problem.

    (You'd probably also find that header() and other PHP functions that send headers wouldn't work in your PHP file, either, giving you the error that output has already started, again because the BOM would have been sent before your PHP started being interpreted.)

提交回复
热议问题