javascript - 我需要访问对象数据(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>
解决方案
你可能不需要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>
推荐阅读
- javascript - 如何修复图形,使其在调整大小时不会自行覆盖
- java - 将对象添加到数量很大的类的更好方法
- .net - ImageSharp - 从流中调整图像大小
- python - 当一个合并列包含 NaN 时使用 pandas.merge_asof
- big-o - sum i=0 -> n (n_i*i)) 的复杂度是多少
- swiftui - SwiftUI - 如何创建全局 @State 变量?
- javascript - 用希伯来语生成 pdf 报告
- python-3.x - 将标题与数据集的其余部分分开
- python - 菜单栏未显示在我的 GUI 窗口中
- javascript - 基于对象属性区分数组的 Javascript 最简洁方法