0

I'm fetching a JSON string from a php file, which returns valid JSON. In essence, the JSON that is returned looks like the following. I removed some data , but I left the data in place that seems to be causing the issue:

{"result": [
  {
    "prop0": 0000000,
    "prop1": 0,
    "prop1Desc": "a valid string",
    "prop2": "00000000000",
    "prop3": "00000000",
    "prop4": "Kiliç",
    "prop5": "00000000",
    "prop6": "a valid string",
    "prop7": 0.0,
    "prop8": "a valid string",
    "prop9": "a valid string",
    "prop10": 0,
    "prop11": 0,
    "prop12": 0.0,
    "prop13": 0.0,
    "prop14": 0.0,
    "prop15": 0.0,
    "prop16": false,
    "prop17": 0.0,
    "prop18": 0
  }
]}

When I use $.getJSON(), the callback-function is not called, rather, the .fail() callback is called.

I got hold of the textStatus, errorThrown and jqXHR.responseText, which are the following:

  • textStatus : parsererror
  • errorThrown : SyntaxError: Ongeldig teken (I guess that's the JS version of the error; from JSON.parse())

Additionally, the jqXHR.responseText:

{"result": [
  {
    "prop0": 0000000,
    "prop1": 0,
    "prop1Desc": "a valid string",
    "prop2": "00000000000",
    "prop3": "00000000",
    "prop4": "Kili�
    "prop5": "00000000",
    "prop6": "a valid string",
    "prop7": 0.0,
    "prop8": "a valid string",
    "prop9": "a valid string",
    "prop10": 0,
    "prop11": 0,
    "prop12": 0.0,
    "prop13": 0.0,
    "prop14": 0.0,
    "prop15": 0.0,
    "prop16": false,
    "prop17": 0.0,
    "prop18": 0
  }
]}

So I think somewhere along the process, jQuery does not like JSON-strings with accented characters. The ç in Kiliç seems to cause trouble, seemingly removing ", at the end of the line as well... Any thoughts on what's going on, and on how I can solve this?


Update

I've looked into the suggestion of @LinkinTED, as the JSON seems to be valid upon inspecting the network. Upon inspection of the PHP file, I realized that it decodes UTF-8 encoded string.. But I'm not sure the following code is valid, as utf8_decode is called upon a json-decoded string (which, in the end, is the valid json).. The PHP file contains the following code to echo the JSON result:

echo utf8_decode(json_decode( $response->opcResponse)->Response->DataResponse[0]->Data);

Removing the function utf8_decode seems to have no effect. Furthermore, I changed the Content-type header, which initially only statee Content-type: application/json. I added ; charset=utf-8, with no effect.

Old:

header('Content-type: application/json');   

New:

header('Content-type: application/json; charset=utf-8');    
Jeroen
  • 345
  • 3
  • 17
  • 1
    It's more likely that the server sends bad data. Look in the network tab and see the pure response there. Also check response headers for the charset and that it matches what the server sends. – JJJ Mar 10 '17 at 09:17
  • 1
    Take a look at this question: http://stackoverflow.com/questions/20694317/json-encode-function-special-characters I believe it's similar to your problem. – GreyRoofPigeon Mar 10 '17 at 09:18
  • 2
    Sounds like an encoding mismatch: The file is being served with encoding X (say, UTF-8), but is actually encoded with Y (say, Windows-1252 or ISO-8859-1). – T.J. Crowder Mar 10 '17 at 09:18
  • 2
    If it's not a network problem, take a look here http://stackoverflow.com/questions/26620/how-to-set-encoding-in-getjson-jquery – Stefano Zanini Mar 10 '17 at 09:19
  • @JJJ: the first JSON-snippet in the question is the JSON I get from the server, which I copied from the network-tab when inspecting network-traffic. – Jeroen Mar 10 '17 at 09:30
  • I've updated the question with attempts to bridge the possible encoding problem. – Jeroen Mar 10 '17 at 13:32

0 Answers0