0

I'm using $.ajax to get JSON data from a REST API.

The problem is that the responseText I get is malformed so I get SyntaxError: JSON.parse: unexpected non-whitespace character error.

I found out that the problem is that the responseText is something like this:

"433
{"Result":{"Locale":"us","ServiceId":1111,"Name":"name",
"HDLevel":"HD Level 
5a0
Unknown","Category":"News","Subcategory":"ne
5b0
ws"}
}"

...

so it can't be parsed correctly to JSON.

I think I need a way to delete all those strings (433, 5a0, 5b0) and also delete new line characters. But I think I need a general way to delete those strings because there are more like them in my responseText and I can't know all names.

Any ideas on how I can do that and obtain a correct JSON? Thanks

edit:

the service uses JSON as format of the returned data and I'm using:

$.ajax({
    type: 'GET',
    url: URL,
    dataType: 'json', 
    success: function(obj) {

    },
    error: function( jqXHR, textStatus, errorThrown ) {

    },
});

I can't access the service server side so I can't edit any php or other languages issues.

Frank
  • 2,083
  • 8
  • 34
  • 52
  • Make sure that the service is actually supposed to return JSON, and then check that you are using the correct character encoding (Maybe you need to ask for json?) – folkol Sep 16 '14 at 09:24
  • You need to respond with properly formed JSON. Maybe post how you encode your data in first place? – dfsq Sep 16 '14 at 09:25
  • This really looks like an encoding issue... id assume the server is using some other encoding... Really looks like the start brackets are coming in URL encoded where it should be "\"{\" ... If this is not a simple encoding issue, then the service is obviously broken... – AnthonyJClink Sep 16 '14 at 09:26

1 Answers1

2

Seems to me more like an unexpected http transfer encoding (chunked). Your actual JSON data is probably fine. Take a look at this question: jquery support Transfer-Encoding:chunked? how

Community
  • 1
  • 1
Sebastian S
  • 4,420
  • 4
  • 34
  • 63
  • I tried to follow that link and use the `jquery.stream` plugin using also `jquery-migrate`, but `$.stream()` gives me the same malformed response. – Frank Sep 16 '14 at 10:19
  • Can you provide an plain http dump? E.g. using telnet or your browser's network console? Those numbers above seem too big for the provided data anyway. – Sebastian S Sep 16 '14 at 11:21