0

有人可以告诉我为什么这个 AJAX Post 请求不起作用。发送的默认 listId 为 0。 savedList 由 ListJSON 和 ListName 组成。代码如下。

$.post('save.php', {"id":listId, "list":JSON.stringify(savedList), 
function(savedListId) {
    listId = savedListId;
    alert('List saved');
    // Refresh to a) get out of edit mode and b) give a bookmarkable URL
    window.location = 'list.html?id='+listId;
});

服务器端如下:

if(empty($_POST['list'])) {
header('HTTP/1.0 400 Bad Request', true, 400);
exit('No list sent');
}

if( ! $decodedList = json_decode($_POST['list'])) {
header('HTTP/1.0 400 Bad Request', true, 400);
exit('List does not decode');
}


if(empty($_POST['id']) || $_POST['id']=="null") {
$sql = "INSERT INTO Lists SET ListJSON = ?, ListName = ?";
$query = $mysqli->prepare($sql);
$query->bind_param("ss", $_POST['list'], $decodedList->listName); 
$query->execute();


header('Content-type: application/json');
exit('"'.$mysqli->insert_id.'"');
} else {
$sql = "UPDATE Lists SET ListJSON = ?, ListName = ? WHERE ListID = ?";
$query = $mysqli->prepare($sql);
$query->bind_param("ssi", $_POST['list'], $decodedList->listName, $_POST['id']);

$query->execute();


header('Content-type: application/json');
exit('"'.(int)$_POST['id'].'"');
}
4

1 回答 1

0

尝试(int)$_POST['id']. 我相信 javascript 将 id 作为字符串发送,但您正在尝试使用 mysqli 将其更新为 int。

于 2013-08-07T18:45:04.530 回答