Read & parse unicode json data not work in IE and

2019-08-10 22:58发布

问题:

I want to read json data file from another domain (CORS). This file contain Unicode data, Like:

{"id":21,"name":"پرسپولیس تهران"}

I used JQuery Ajax function like this:

$.ajax({
    type: "GET",
    cache: false,
    url: SDomain + 'XML/TeamXMLCache/' + filename + '.json',
    dataType: "json",
    contentType: 'application/json; charset=UTF-8',
    success: parseXml,
    error: function (request, status, errorThrown) {
        console.log(request);
        console.log(status);
        console.log(errorThrown);
    }
});

And this work me fine in Chrome 39 but not working in IE11 & FF32.

In IE I get parsererror error.

For more information:

  • responseText is:{"id":21,"name":"~13~HD3 *G1'F"}
  • readyState:4
  • statusText:"OK"
  • status:200

And finally my config file is:

<configuration>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

So please tell me why? I'm not a beginner!

Real sample is: My real sample

回答1:

When I loaded http://static.persiangulfcup.org/XML/TeamXMLCache/21-%D9%BE%D8%B1%D8%B3%D9%BE%D9%88%D9%84%DB%8C%D8%B3-%D8%AA%D9%87%D8%B1%D8%A7%D9%86.json?_=1417448329770 with Firefox in a browser window, it displayed the JSON fine but indicated in document info that the encoding is UTF-16LE and not UTF-8 as indicated in your post. Based on that I think you need to make sure the data is correctly UTF-8 encoded or you need to make sure it is served with a content type header indicating the real encoding.



回答2:

Thanks to @Martin for correct answer.

Problem was when I create my json file. I used System.Text.Encoding.Unicode instead of System.Text.Encoding.UTF8 to save json files in my server.

But my new question is how do you understand that my file is under UTF-16LE format? Please post as answer then I can mark it as correct answer.