首页 > 解决方案 > 我需要访问对象数据(id-title-body)

问题描述

我需要访问 data.data[0].title

打印时

数据.数据[0]

我得到所有对象数据,但在打印时

数据.data[0].title

我无法访问任何数据

// 这是我的 ajax 脚本

   <script>
        $.ajax({
            url: 'http://127.0.0.1:8000/api/posts',
            dataType: 'json',
            type: 'GET',

            success: function(data) {
                $('#test').html( JSON.stringify( data.data[0].title ) );

            }
        })
    </script>


  <p id="test" ></p>


 

标签: javascriptjsonajax

解决方案


你可能不需要JSON.stringify(). @illusion 是对的!我猜您的响应对象结构类似于:

{
    "data": [{
        "title": "This is the title"
    }]
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
        $.ajax({
            url: 'https://extendsclass.com/api/json-storage/bin/ebbfccb',  //replace this with your api URI
            dataType: 'json',
            type: 'GET',

            success: function(data) {
           $('#test').html(data.data[0].title);

            }
        })
    </script>


  <p id="test" ></p>


推荐阅读