我有这样的API响应:
{
"status": 200,
"message": "OK",
"data": {
"total": 5
}
}
我想将total
上述响应中的值写入5
HTML。当我创建js文件以获取响应时,HTML中的结果为空。
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>
<div class="inner">
<h3 id="total"></h3>
<p>Total</p>
</div>
$(function() {
$.ajax({
url: 'https://api-url',
type: 'GET',
dataType: 'json',
success: function(data) {
$.each(data, function(value) {
//append each row data to datatable
var row = value.total
$('#total').append(row);
});
}
})
})
您知道如何total
从HTML的API中显示所需的内容吗?谢谢
您不需要在each()
这里循环,因为响应是单个对象,而不是数组。因此,你可以访问data.value
,并将其设置为text()
的#total
,就像这样:
$(function() {
$.ajax({
url: 'https://api-url',
type: 'GET',
dataType: 'json',
success: function(response) {
var total = response.data.total;
$('#total').text(total);
}
})
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句