我犹豫要不要问,因为还有很多关于这个主题的其他帖子(one and two例如),但这些帖子中的解决方案似乎都不适用于我。
我试图将 JSON 编码的对象从页面传递到 PHP 控制器,然后返回一些信息。
如果我在 Firebug 中观看,我可以看到在“Post”选项卡下发送的对象,但是当我打印出 $_GET、$_POST 和 $_REQUEST 数组时,我看不到任何有关 json 对象的信息。 $_GET 数组至少显示了我的“update-player”查询字符串,但是 POST 是空的,而 REQUEST 只显示了我拥有的一些本地 cookie。
这是我的 jQuery 代码。正如您所看到的,我现在正在对 JSON 进行硬编码,目的是让我有一个 jQuery 方法来更新本地对象。
function sendPlayerUpdate(row, col) {
var playerinfo = [
{
"id": 1,
"row": row,
"col": col
}
];
alert(playerinfo[0].id); //debugging
$.ajax({
type: 'POST',
url:"controller.php?a=update-player",
//data: $.toJSON(playerinfo[0],
data: { json: JSON.stringify(playerinfo) },
contentType: "application/json",
success: function (){
},
dataType: 'json'
});
};
我处理请求的相应 PHP 代码:
// update player information from AJAX POST
case "update-player":
if (isset($_POST['json'])) echo "json received\n\n";
else echo "json not received\n\n";
echo "GET VARIABLES\n";
print_r($_GET);
echo "\n\nPOST VARIABLES\n";
print_r($_POST);
echo "\n\nREQUEST VARIABLES\n";
print_r($_REQUEST);
而且,我在 Firebug 中看到的是:
json not received
GET VARIABLES
Array
(
[a] => update-player
)
POST VARIABLES
Array
(
)
REQUEST VARIABLES
Array
(
[a] => update-player
(local cookies)
)