代碼中的index.php:無法訪問哈希值
內trash.php<!DOCTYPE html>
<html lang="en-us" dir="ltr">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".trashitem").click(function(e) {
e.preventDefault();
var trashlink = $(this);
var res = confirm("Are you sure you want to trash the selected item?");
if (res != false) {
var trashurl = trashlink.attr("href");
var trashid = trashlink.data("id");
$.ajax({
url: trashurl,
type: "POST",
data: { "trashid": trashid },
datatype: "json",
success: function(response) {
console.log(response); // Output: {"success":true,"error":false}
console.log(response.success); // Output: undefined
if (response.success == true) {
trashlink.parents("li").fadeOut(300, function() {
trashlink.parents("li").remove();
});
}
}
});
}
});
});
</script>
</head>
<body>
<ul>
<li><a class="trashitem text-color-alert" href="trash.php" data-id="20">Trash</a></li>
<li>This is undeletable item.</li>
</ul>
</body>
</html>
代碼是:
<?php
$id = $_POST['trashid'];
$error = false;
$success = false;
if ($id) {
$success = trash_post($id);
$response = array(
'success' => $success,
'error' => $error
);
}
echo json_encode($response);
function trash_post($target_id) {
return true;
}
請看第一代碼片段中的index.php其中:
console.log(response); // Output: {"success":true,"error":false}
但是當我想訪問success
:
console.log(response.success); // undefined
爲什麼它給我undefined
而不是true
?我遇到過類似於我的Stackoverflow上的一些帖子,但它們對我沒有幫助。
做了一些修改,以更好地解釋這個問題,這個問題。 –