简体   繁体   English

console.log JSON数据?

[英]console.log JSON data?

Problem 问题

I'm trying to log the data returned from a JSON call, and console.log(jsonData) doesn't seem to be working. 我正在尝试记录从JSON调用返回的数据,并且console.log(jsonData)似乎不起作用。

Code

$(document).ready(function() {
    $("#users li a:first-child").click(function() {

        var id = this.href.replace(/.*=/, "");
        this.id = "delete_link_" + id;

        if(confirm("Are you sure you want to delete this user?"))
        {
            $.getJSON("delete.php?ajax=true&id=" + id, function(data) {
                console.log(data.success);
            });
        }

        return false;
    });
});

Returned by delete.php delete.php返回

{"id": $id, "success": 1} upon success. {"id": $id, "success": 1}成功后。
{"id": $id, "success": 0, "error": "Could not delete user."} upon failure. {"id": $id, "success": 0, "error": "Could not delete user."}失败时。

Your JSON isn't valid: 您的JSON无效:

{'id' : 2, 'success' : 1}

It should have double quotes: 它应该有双引号:

{"id" : 2, "success" : 1}

Based on that I think you're manually building the JSON string, I would suggest using json_encode() instead: 基于此我认为你是手动构建JSON字符串,我建议使用json_encode()代替:

$result = new stdClass;
$result->id = 2;
$result->success = 1;

echo json_encode($result);

Outputs 输出

{"id":2,"success":1}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM