1

My problem is html content not being parsed with JSON.parse .

  1. I am getting input from a content editable div.

    var content = $("#contentInput").html();

  2. I am sending it to server after JSON.stringify

    var dataToServer= JSON.stringify(content); //sending an object

  3. The same content I am getting from server, and I attempting to parse it.;

    var dataFromServer = JSON.parse(content);

Here my problem raises parser throwing error. content of html data receiving as it is.

I was tried eval but after reading the some articles I was withdrawn my decision.

How to resolve the problem.

9
  • 2
    Why are you JSON.stringifying a string? Commented Oct 5, 2012 at 5:55
  • @Musa: That's what suprised me too :) Commented Oct 5, 2012 at 5:57
  • i was sending an object with content to server that's way JSON.stringify Commented Oct 5, 2012 at 6:24
  • How are you sending the data to the server? I'm pretty sure you don't need JSON at all! Commented Oct 5, 2012 at 6:27
  • i have some other fields to send with content i must need json Commented Oct 5, 2012 at 6:32

2 Answers 2

2

Here is fiidle demo of working code.

The problem is your server corrupting the content. Please check out your server code, it may add some non printable charaecters.

JSON.parse fails if the content not in JSON format. It strictly follow the rules of jsonstring to parse it( I mean double quotations and other /).

Your server may be adding some other unuseful characters to content or jsonstring.

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

1 Comment

How do you check for / remove these non printable characters?
0

JSON.stringify expects a javascript object. Try like this:

var content = $("#contentInput").html();
var dataToServer = JSON.stringify({ html: content });
...
var dataFromServer = JSON.parse(dataToServer);
var content = dataFromServer.html;

3 Comments

its not working yar! thats way i was posted the question. Please the question properly.
stringify on a string is perfectly legal. There may not be much of a point in doing it if you know you're always dealing with just strings, but there's nothing wrong with it: JSON.parse(JSON.stringify('foo')) === 'foo'.
@rameshbabu, what error do you get? Does the browser that you are using support the JSON.stringify method?

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.