【发布时间】:2012-07-27 11:56:48
【问题描述】:
我的代码运行良好,除了“userName”,由于某种原因,通过 JSON 发送字符串不会发布到表,它什么也不发送。
谁能看出问题所在?
jquery
lowestScoreId = 1;
userPoints = 50;
userName = "ted";
$.getJSON("functions/updateHighScores.php", {lowestScoreId: lowestScoreId, userPoints: userPoints, userName: userName}, function(data) {
$('#notes').text(data.userName); //for testing
});
php
lowestScoreId = json_decode($_GET['lowestScoreId']);
$userName = json_decode($_GET['userName']);
$userPoints = json_decode($_GET['userPoints']);
include 'config.php';
$currentTime = time();
mysql_query("UPDATE highScores
SET `name` = '$userName',
`score` = '$userPoints',
`date` = '$currentTime'
WHERE id='$lowestScoreId'");
echo json_encode(array("userName" => $userName)); // for testing
【问题讨论】:
-
我看不到
$obj的任何用法。 -
我想你把这一切都搞混了......应该发送 1 个 JSON 对象而不是 3 个?它应该类似于
$obj=json_decode($_GET['jsonObj'])以及从 JSON 对象中获取的其余值。另一件事......你没有清理你的输入并将其直接输入你的 MySQL 数据库。 -
嗯,好的。感谢您的提示。我还是有点新,所以现在就开始工作吧。
-
在这里看看我的回答:stackoverflow.com/a/11606240/1031312
-
@user1555800 这实际上只是对 getJSON 的混淆。 jquery 中的 getJSON 表示“执行 GET 请求,我希望服务器的结果是 JSON 格式”。响应服务器应将数据作为标准 GET 请求(即 /something?param1=yes¶m2=25)处理,并且是预期的发回 json (所以你最后的 json_encode 是正确的,但发送到服务器的数据是标准的 GET 数据,而不是 json)