json - 如何从ajax结果中打印id

标签 json ajax

如何从ajax数据打印id

data.id未定义

$.ajax({
    url: "userdetails",
    type: "get",
    success: function (data) {
        alert(data);
        console.log($(this).serialize());
        $("#ajaxdata").append(data);
        alert(data.id)
    }
});

数据值低于

[{
    "id": 28,
    "firstname": "asddsf",
    "lastname": "sss",
    "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5a1b0a7a7acb8bab9fbb6bda7bca6a1bcb4bb95a7bab4a7a6bcbbb6fbb6bab8" rel="noreferrer noopener nofollow">[email protected]</a>",
    "role_id": 1,
    "status": "Active"
}, {
    "id": 87,
    "firstname": "df",
    "lastname": "dsfsd",
    "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f3809795809795b397959780dd9b9499" rel="noreferrer noopener nofollow">[email protected]</a>",
    "role_id": 1,
    "status": "Active"
}]

最佳答案

如果未指定数据类型或服务器未在响应 header 中提及该数据类型,则响应将不会被视为 JSON。

试试这个

$.ajax({
    url: "userdetails",
    type: "get",
    dataType: "json",
    success: function (data) {
        alert(data);
        console.log($(this).serialize());
        $("#ajaxdata").append(data);
        alert(data.id)
    }
});

更新:

响应看起来像一个对象数组。您需要尝试 alert(data[0].id)

关于json - 如何从ajax结果中打印id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32515658/

相关文章:

java - 使用 Jackson 反序列化接口(interface),其中接口(interface)实现在序列化对象中指定

javascript - 使 ajax 响应全局并可在所有路由中使用

javascript - TypeError : this. $.ajax 请求状态为 null

具有 RESTful JSON 服务、HTML5 和 jQuery ajax 的 Java Web 框架

php - 如何在响应中返回数组

java - 从服务器接收的字符串创建 JSON 对象

json - 如何用 aeson 解析嵌套的 JSON

javascript - 如果没有用户查询的地名,我无法显示 noroom 组件

javascript - 使用 JSPdf 生成将整个 html 页面导出为 pdf

php - jQuery 在 AJAX 加载页面中不起作用