php - Javascript:: Convert JSON string post callback to pure javascript array -
php - Javascript:: Convert JSON string post callback to pure javascript array -
i wrote php function check array duplicates.
if array have no duplicates returned array json string otherwise print (as pure text) duplicate(/d) value(/s).
my php function working perfectly.
now want convert returned json string , utilize pure javascript array (off-course assuming there no duplicates).
here hezi.php file:
if($_server['request_method']==='post') { $gangina=array ( 'douplicatetesting', '1', '2', '3', 'douplicatetesting', '4', '3' ); function dupvalidator() { global $gangina; if(count($gangina)===count(array_unique($gangina))) { header('content-type: application/json'); echo json_encode($gangina); } else { header('content-type: text/plain'); echo'opps! array contains duplicates'."\r\n".str_repeat(chr(45),36)."\r\n\r\n"; print_r(array_diff_key($gangina,array_unique($gangina))); exit; } } dupvalidator(); } this index.htm file (without javascript ajax post callback):
<pre id=mystringify></pre> <script> var callbackarray= [ { value:1, type:wavfile } , { value:2, type:aifffile } ]; document.getelementbyid('mystringify').innerhtml=json.stringify(callbackarray,null,4); </script> in index.htm wrote dummy javascript array prototype easier thought i'm accomplish here.
so pure javascript array :
var callbackarray=[{value:1,type:wavfile},{value:2,type:aifffile}];
would created via post ajax callback request:
var callbackarray=<!--post ajax callback request--> i know how via jquery want pure javascript.
use javascript function json.parse(teststring). documentation mentions eval() utilize older browsers noted, shouldn't utilize if possible prevent potential security problems.
var obj = json.parse(text); here working fiddle.
javascript php arrays ajax json
Comments
Post a Comment