嗯,除了一串字节,你不能发送任何东西。 “发送数组”是通过序列化(制作对象的字符串表示)数组并发送来完成的。
然后服务器将解析字符串并从中重新构建内存对象。
所以将[1,2,3] 发送到 PHP 可能会像这样发生:
var a = [1,2,3],
xmlhttp = new XMLHttpRequest;
xmlhttp.open( "POST", "test.php" );
xmlhttp.setRequestHeader( "Content-Type", "application/json" );
xmlhttp.send( '[1,2,3]' ); //Note that it's a string.
//This manual step could have been replaced with JSON.stringify(a)
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)
顺便说一句,使用 jQuery 你会这样做:
$.post( "test.php", JSON.stringify(a) );