I get this response from an Ajax request. Javascript seems to intepret it as a string. (When I say alert this.responseText, the whole string is shown)
How can i convert it to a javascript object (JSON)?
{"response": {
"success": "The activity has been removed",
"message": "0"
}
}
I am not using jquery.
-
check out stackoverflow.com/questions/45015/…Detect– Detect2010年10月06日 17:07:33 +00:00Commented Oct 6, 2010 at 17:07
-
using prototype or native javascript?Detect– Detect2010年10月06日 17:09:23 +00:00Commented Oct 6, 2010 at 17:09
-
The reason might be "http status" code. Check the http status code (via F12 in IE or Firebug in FF) to see if it's 200 (=OK) or not.Tohid– Tohid2012年09月07日 13:35:21 +00:00Commented Sep 7, 2012 at 13:35
3 Answers 3
If you use jQuery, JSON.parse(this.responseString); or jQuery.parseJSON(this.responseString); should work.
answered Oct 6, 2010 at 17:17
knepe
3251 gold badge3 silver badges9 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
It's not the safest thing in the world, but you can do this:
var value = null, txt = this.responseText;
eval("value = (" + txt + ")");
It might be a little safer to do:
var value = null, txt = this.responseText;
!function(window) { eval("value = (" + txt + ")"); }();
but there are still all sorts of potential hacks. You're better off using a library.
answered Oct 6, 2010 at 17:31
Pointy
415k62 gold badges600 silver badges633 bronze badges
1 Comment
robert
target users are blackberry devices. Most libraries don't work (well or at all) with older BB. I am trying XUI, but have not figured out it's JSON capabilities yet.
answered Oct 6, 2010 at 17:08
mway
4,3573 gold badges28 silver badges37 bronze badges
Comments
lang-js