Well, you cannot send anything but a string of bytes. "Sending arrays" is done by serializing (creating a string representation of the objects) of the array and sending it. Then the server will analyze the string and re-build the memory objects inside it.
Therefore, sending [1,2,3]to PHP can happen like this:
var a = [1,2,3],
xmlhttp = new XMLHttpRequest;
xmlhttp.open( "POST", "test.php" );
xmlhttp.setRequestHeader( "Content-Type", "application/json" );
xmlhttp.send( '[1,2,3]' );
test.php:
$data = file_get_contents( "php://input" ); //$data is now the string '[1,2,3]';
$data = json_decode( $data ); //$data is now a php array array(1,2,3)
Btw, with jQuery you would just do:
$.post( "test.php", JSON.stringify(a) );
source
share