2

I have create one JSON file through PowerShell and place it on serve.

When i access that JOSN file through $.getJSON it works fine in crome and IE browser but when i access that JSON file in Firefox i got error of

JSON.parse: unexpected character at line 1 column 1 of the JSON data

Header:

enter image description here

Response:

enter image description here

What should be issue and how to fix it in firefox?

Kaushal Khamar
  • 2,097
  • 1
  • 17
  • 23
  • http://stackoverflow.com/questions/25743994/syntaxerror-json-parse-unexpected-character-at-line-1-column-1-of-the-json-dat – Rino Raj Feb 15 '16 at 05:52
  • It's clearly a character set identification issue, the server appears to be replying with UTF-16. What is the `Content-Type` header in the response? – T.J. Crowder Feb 15 '16 at 05:53
  • @Rino in that question the issues was relative to PHP. And i have tried that all solution but no luck. – Kaushal Khamar Feb 15 '16 at 05:54
  • @T.J. I am requesting through $.getJSON(Url) in which no need to pass Content-Type – Kaushal Khamar Feb 15 '16 at 05:55
  • 1
    @DOM: There's always a need for the server to correctly identify the character set of a resource when it supplies it. More: [*The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)*](http://www.joelonsoftware.com/articles/Unicode.html) Again: What is the `Content-Type` header **in the response**. You can find it in the Network tab. – T.J. Crowder Feb 15 '16 at 05:55
  • I have checked in header in which i have found that Content-Type is "text/plain" – Kaushal Khamar Feb 15 '16 at 05:57

1 Answers1

1

You've said that the server sends that JSON back with Content-Type: text/plain. The data appears to be in UTF-16 (probably, that's based on the screenshot), but the default charset for text/plain is us-ascii (see §4.1.2 of RFC2046):

4.1.2. Charset Parameter

A critical parameter that may be specified in the Content-Type field for "text/plain" data is the character set. This is specified with a "charset" parameter, as in:

Content-type: text/plain; charset=iso-8859-1

Unlike some other parameter values, the values of the charset parameter are NOT case sensitive. The default character set, which must be assumed in the absence of a charset parameter, is US-ASCII.

So, you need to change the response from the server such that it correctly identifies the character set being used, e.g. Content-Type: text/plain; charset=UTF-16 (obviously ensuring first that that is, in fact, the charset of the resource).


I'll just note that, from what I can make out of the JSON, it looks like it's primarily in a western script. If so, UTF-16 is unusual and inefficient choice, you'd probably be better off with UTF-8. But I only have a small fragment of the text to work from.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Hi T.J. thanks for answer. I have update the question. Can you please look into it. i still need to change response from server? and it also works in chrome and IE. Issue only occur in Firefox. – Kaushal Khamar Feb 15 '16 at 06:19
  • @DOM: Yup. If the server were specifying the charset, it would be on that `Content-Type` header line. (For comparison, look at the `Content-Type` SO serves with this page.) Since it isn't, Firefox appears to be using the default according to the RFC (US-ASCII) while IE appears to see the BOM (the first two bytes of the response) and switch to treating it as UTF-16. – T.J. Crowder Feb 15 '16 at 06:25