0

I am using javascript and php and need to pass some HTML in the JSON variable (PHP->JS).

Unfortunately, due to some environmental constraints I am limited in the jQuery I can use. Using things such as jQuery.parseJSON(str) and $.parseJSON(str) throw unexpected token errors.

Therefor I need a purely javascript approach to handling html in a JSON variable. Currently, the HTML string is just printed as a string on the page, though I need it to take effect as HTML.

My JS code is as follows:

document.getElementById("activeDescription").innerHTML = response['description'];

and the results ends up just being text on the HTML page as follows:

<p>helloworld</p>

whereas I expect just

helloword

to be displayed on the HTML page. On alert(response['description']) I receive

&lt;p&gt;&lt;span class=&quot;

EDIT

When I use

jQuery.parseJSON('{"name":"John"}');

everything is peachy but this code

jQuery.parseJSON(response['description']);

gives me an "Uncaught SyntaxError: Unexpected token & " error

8
  • 1
    what is returned by response['description']? It's not clear what you expected to happen. Commented Jan 4, 2013 at 14:48
  • unexpected token errors? are you json_encodeing your php arrays? Commented Jan 4, 2013 at 14:49
  • so to be clear, you just need a pure javascript way of decoding html specific characters? It sounds like you are getting &lt; and &gt; in your string Commented Jan 4, 2013 at 14:49
  • 12
    You have a 0% accepted rate, please accept some answers on your older questions. Commented Jan 4, 2013 at 14:49
  • 1
    alert(response['description']); .. are the characters like <p> or '&gt;p&lt; ? Commented Jan 4, 2013 at 14:49

1 Answer 1

1

Most probably a encoding problem. You can fix by decoding the characters.

In javascript:

var div = document.createElement('div');
div.innerHTML = encoded;
var decoded = div.firstChild.nodeValue;

In PHP, look at this link: http://www.php.net/manual/en/function.htmlentities.php

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

Comments

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.