0

I am trying to get out a JSON object, but I end up with getting all the HTML as well.

    alert("5 " + request.responseText);
    var JSONObject = eval( + "(" + request.responseText + ")" );
    alert("text1: " + JSONObject.name);

So, the first line prints pure html code in an alert. Is it supposed to do that, or is the responseText supposed to only be the JSON Object? The second line does not work, so the third line doesn't print.

I've seen examples where they just use eval on the responseText as I do, but in my case it doesn't work... Any suggestions?

3
  • 4
    Your server determines what sort of "crap" you get back. Commented May 29, 2012 at 20:48
  • if request.responseText is HTML, it cannot be evaled into an object.. Commented May 29, 2012 at 20:48
  • 1
    "Eval" is Evil, Dude: squdgy.wordpress.com/2011/10/04/is-javascript-eval-really-evil. You're much better off using something like a jQuery "getJSON()". Better yet, just create your JSON object on the server. IMHO... Commented May 29, 2012 at 20:50

2 Answers 2

1

Try method

jQuery.ParseJSON(request.responceText);

But first make sure your server send data in json format?

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

Comments

0

Sounds like the server side's spewing HTML and JSON in one response. I'd start by checking the call in a browser or a proxy like fiddler to make sure it's only JSON coming back.

4 Comments

Looks like its not only JSON coming back here. Does it exist an easy way of stripping the responseText to only JSON?
If you've got the JSON inside an HTML element, you can use something like JQuery to get just the bit that's the JSON you're after out of the responseXml and then just parse that JSON string.
I am doing this now: var json = $.getJSON(URL); alert(json); alert(json.name); (This is three lines of code, but its gonna look like one) The first alert prints: [object Object] The second alert prints: undefined What does that mean?
That it still can't parse your JSON. You'll need to isolate the JSON from the HTML before you parse it as I said above.

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.