您可以将数据作为JSON http://www.w3schools.com/json/目的。假设您的 JSON 对象如下:
var stuff ={'key1':'value1','key2':'value2'};
您可以通过两种方式将此对象传递给 php 代码:
1. 将对象作为字符串传递:
AJAX 调用:
$.ajax({
type : 'POST',
url : 'result.php',
data : {result:JSON.stringify(stuff)},
success : function(response) {
alert(response);
}
});
您可以处理传递给的数据result.php
as :
$data = $_POST["result"];
$data = json_decode("$data", true);
//just echo an item in the array
echo "key1 : ".$data["key1"];
2.直接传递对象:
AJAX 调用:
$.ajax({
type : 'POST',
url : 'result.php',
data : stuff,
success : function(response) {
alert(response);
}
});
直接处理数据result.php
from $_POST
数组为:
//just echo an item in the array
echo "key1 : ".$_POST["key1"];
这里我推荐第二种方法。但你应该两者都尝试一下:-)