so im creating this situation where a count down happens, when it counts down to zero, a ajax post is fired off, php thens returns a json_encoded array and i would like to loop threw this array and .remove things that need to be removed and .appendTo things that need to be added. however, i seem to have a hard time accessing an element from that json object returned from the server…. im getting undefined in the console log
jquery:
$.ajax({
url: "morphelo/splash.php",
type: "POST",
data: "x=<?=md5('countdown')?>&CountdownType="+type+"&id="+ID+"&what=<?md5('drop')?>",
datatype: "html",
success: function(data){
data = // {"19e0fa430b5a0437b87cf5bf7a365011":{"clutch-1337653726-445648193-0a6e039a028dd9a64239473ff08c82c6":"sub-clu-li**clutch-1337653726-445648193-0a6e039a028dd9a64239473ff08c82c6"}}
var x = type.split('/');// 19e0fa430b5a0437b87cf5bf7a365011/unclock
var m = x[0]; // 19e0fa430b5a0437b87cf5bf7a365011
var y = $.parseJSON(data);
console.log(y);
console.log(m);
console.log(data.m) // but it says undefinded.....
}
});
Your
data.mdoesn’t work, becausedatais a string.You probably meant to access
min the decoded version ofdata, which you calledy:I’m using
[m]instead of.mbecausemstarts with numerics.Btw
If you’re always going to decode the HTML into JSON, you can also set the
datatypeof the$.ajaxoptions to'json'.