php - Send array in ajax query -


i'm trying send array javascript php $.post() method of jquery.

i've tried jquery.serialize(), jquery.serializearray(), json.stringify() , of them didn't work.

here code:

$.post("ajax/"+action+"_xml.php",{'array': array},function(data){console.log(data);}); 

the array looks :

array["type"] array["vars"]["name"] array["vars"]["email"] 

array["vars"] has more 2 elements.

the result in php $_post variable empty array (length 0).

i suggest following structure on data pass:

javascript:

var dto = {      type: [1,2,3],     vars: {           name: 'foo',         email: 'foo@bar.com'     } };  var stringifieddata = json.stringify(dto);   // result in: //{"type":[1,2,3],"vars":{"name":"foo","email":"foo@bar.com"}}   $.post("ajax/"+action+"_xml.php",{'dto': stringifieddata },function(data){     console.log(data); }); 

php:

header('cache-control: no-cache, must-revalidate'); header('expires: mon, 26 jul 1997 05:00:00 gmt'); header('content-type: application/json');  $dto = $_post['dto'];  if(isset($dto)) {     $assocresult = json_decode($dto, true);     var_dump($assocresult); //do stuff $assocresult here } 

passing true second argument json_decode make return associative array.

http://php.net/manual/en/function.json-decode.php


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -