Why my code returns emptiness:

var test = '{"1500536354128591697-140-4-709-130969827":{"value":"1500536352145246223-50-1-26193-1898645603"},"1476252684967526706-157-4-26312-181217637":{"value":"1476252684887081320-151-1-26312-2532790135"},"1476252684985803841-159-4-26312-1052842564":{"value":"1476252684887081320-151-1-26312-2532790135"},"1486808888647977659-59-4-582-4256386927":{"value":"1486808888553727044-58-1-582-1794206325"},"1493449722500199519-187-4-709-3016271615":{"value":"1493449708210015275-145-1-26193-4061830634"},"1477640042446460021-164-4-26341-378056455":{"value":"1477640042385839468-158-1-26341-511999530"},"1482841748690991909-87-4-582-2966223399":{"value":"1482841748582930127-86-1-582-755784562"},"1476941827335692438-100-4-582-657711144":{"value":"1476941827183313507-73-1-582-3215889431"},"1477640042435309689-162-4-26341-3795155381":{"value":"1477640042385839468-158-1-26341-511999530"}}'; $.ajax({ type: "POST", url: "index.php", data: test, dataType: 'JSON', cache: false, success: function(data) { console.log(data); } }); 

index.php:

 print_r($_POST); 

    2 answers 2

    Remove the quotes in the var test . Because you are sending as text and not json

     var test = {"1500536354128591697-140-4-709-130969827":{"value":"1500536352145246223-50-1-26193-1898645603"},"1476252684967526706-157-4-26312-181217637":{"value":"1476252684887081320-151-1-26312-2532790135"},"1476252684985803841-159-4-26312-1052842564":{"value":"1476252684887081320-151-1-26312-2532790135"},"1486808888647977659-59-4-582-4256386927":{"value":"1486808888553727044-58-1-582-1794206325"},"1493449722500199519-187-4-709-3016271615":{"value":"1493449708210015275-145-1-26193-4061830634"},"1477640042446460021-164-4-26341-378056455":{"value":"1477640042385839468-158-1-26341-511999530"},"1482841748690991909-87-4-582-2966223399":{"value":"1482841748582930127-86-1-582-755784562"},"1476941827335692438-100-4-582-657711144":{"value":"1476941827183313507-73-1-582-3215889431"},"1477640042435309689-162-4-26341-3795155381":{"value":"1477640042385839468-158-1-26341-511999530"}}; $.ajax({ type: "POST", url: "index.php", data: test, dataType: 'JSON', cache: false, success: function(data) { console.log(data); } }); 
    • Thanks helped. - newProgrammer
    • @newProgrammer You're welcome ... - KYRAN

    By sending a request in this way, you send JSON in the body of a POST request, to get it on the server, read the stream php://input like this:

     $json = json_decode(file_get_contents('php://input')); var_dump($json);