无法访问哈希值

Cannot Access Hash Values

里面的代码index.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>

trash.php 中的代码是:

<?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?我在 Whosebug 上看到过一些与我类似的帖子,但它们对我没有帮助。

可能 response 只是一个字符串而不是 Object

尝试

var a = JSON.parse(response);
console.log(a.success);

或尝试将其添加到 php 文件的顶部

header('Content-Type: application/json');

此设置 php 文件的内容类型为 JSON

试试这个

response['success'] == true

而不是

response.success == true

尽量不要为您的响应使用成功或错误标志,而是使用状态代码并构建正确的响应。您可以使用 http_response_code() 函数来做到这一点。例如:

<?php
http_response_code(200);
?>

然后要正确处理 ajax 调用中的响应,请执行以下操作:

$.ajax({
    url: trashurl,
    type: "POST",
    data: { "trashid": trashid },
    datatype: "json"
})
  .done(function (response) { //response status code is 200
       trashlink.parents("li").fadeOut(300, function() {
           trashlink.parents("li").remove();
       });
  })
  .fail(function (xh) {
      //Handle failed response
  });