0

I try to print JSON response in my html document so I do:

respdiv.innerHTML = '<pre><code>' + xhr.responseText + '</code></pre>';

The problem is my response is printed like this :

{ "error": "Sc\\u00e9nario invalide" }

I want my response to be printed like this :

{ "error": "Scénario invalide" }

How I get this?

2
  • 3
    It sounds like you need to fix the problem on the server. Can you make the server return the response in UTF-8 or not escape the \ characer? Commented Jul 13, 2014 at 15:59
  • I will try to figure out how to do it in the server side. Commented Jul 13, 2014 at 16:01

1 Answer 1

1

You can delegate all the work to JSON.parse() then unescape the characters:

var responseText = unescape(JSON.parse('"' + xhr.responseText + '"'));
respdiv.innerHTML = '<pre><code>' + responseText + '</code></pre>';

But as @p.s.w.g suggested, I would advice fixing it on server side.

Sign up to request clarification or add additional context in comments.

4 Comments

Your code is not working but I managed to get the result using your idea. Thanks!
You can provide the snippet that made it work in order to help future readers, think community :)
I changed the code provided in the answer but not accepcted
Sure it won't work on the provided fiddle with my hook just because you are trying to parse a JSON object ({ "error": "Sc\\u00e9nario invalide" }) while I wrote a snippet to only unescape a straightforward string object. You got it? Take a look at this version of you fiddle, jsfiddle.net/fsh6s/1 and compare it to yours. You will see the difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.