javascript - Ajax and JSON arrays -
i'm trying use json data format jquery and, well, surprising if worked.
in remoteserviceengine.php, have this:
$jresponse[0] = json_encode(array("jaction" => "add", "jobject" => "listcountries", "jbody" => "test")); $json_data = json_encode(array("jrequeststate" => $jrequeststate, "jmessage" => $jmessage, "jresponse" => $jresponse)); echo $json_data;
and how handled in js:
success: function(remoteresponse){ switch(remoteresponse.jrequeststate) { case 0: $("#removeservicemessage").fadein(2000).html('<div class="remoteerror">'+remoteresponse.jmessage+'</div>').fadeout(2000); break; case 1: $("#removeservicemessage").fadein(2000).html('<div class="remotesuccess"><b>success:</b> '+remoteresponse.jmessage+'</div>').fadeout(2000); (i = 0; < remoteresponse.jresponse.length; i++) { switch(remoteresponse.jresponse[i].jaction) { case "add": $("#"+remoteresponse.jresponse[i].jobject).fadein(1000).append(remoteresponse.jresponse[i].jbody); break; case "remove": $("#"+remoteresponse.jresponse[i].jobject).fadeout(1000); break; case "update": $("#"+remoteresponse.jresponse[i].jobject).fadein(1000).html(remoteresponse.jresponse[i].jbody); break; default: alert(remoteresponse.jresponse[i]); break; } } break; } }
the whole problem cannot access successful content. $jrequeststate = 1 , forementioned $jresponse[0], switch goes directly onto default , output get:
{"jaction":"add","jobject":"listcountries","jbody":"test"}
but cannot figure out how access these elements. tried with:
alert(remoteresponse.jresponse[i]['jaction']);
and
alert(remoteresponse.jresponse[i][0]); //yeah, that's kinda stupid solution, well...
since i've never used json jquery, can't figure out how deal that. help, anybody?
like adeneo said in comment jsonencode twice. php code should like:
$jresponse[0] = array("jaction" => "add", "jobject" => "listcountries", "jbody" => "test"); $json_data = json_encode(array("jrequeststate" => $jrequeststate, "jmessage" => $jmessage, "jresponse" => $jresponse)); echo $json_data;
Comments
Post a Comment