weixin_33724570 2013-01-03 15:12 采纳率: 0%
浏览 332

console.log JSON数据?

Problem

I'm trying to log the data returned from a JSON call, and console.log(jsonData) doesn't seem to be working.

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

{"id": $id, "success": 1} upon success.
{"id": $id, "success": 0, "error": "Could not delete user."} upon failure.

  • 写回答

1条回答 默认 最新

  • YaoRaoLov 2013-01-03 15:22
    关注

    Your JSON isn't valid:

    {'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:

    $result = new stdClass;
    $result->id = 2;
    $result->success = 1;
    
    echo json_encode($result);
    

    Outputs

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

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)