javascript - .then((resolve) => resolve.json()) 的含义
问题描述
很抱歉这个基本问题,我对承诺等很陌生。只是想知道第 3 行的含义是什么?
window.onload = (function(){
fetch('http://localhost:8080/CarSales/rest/cars')
.then((resolve) => resolve.json())
.then((data) => {
var output = '';
data.forEach(function(cars){
output += '<tr><td>'+cars.make+'</td><td>'
+cars.model+'</td><td>'+cars.year+'</td><td>'
+cars.engine+'</td></tr>';
});
document.getElementById('table-body').innerHTML = output;
})
})
解决方案
.then((resolve) => resolve.json())
api返回响应,fetch
但是为了解析json响应,需要调用json函数。这也返回了一个最终返回 json 数据的 promise。
因此,这一行正在解析 json。
推荐阅读
- machine-learning - 如何使用训练有素的神经网络?
- java - Spring - How to save Hibernate data to SQL server?
- python - 使用 Django Rest Framework 序列化程序为外键关系创建方法
- c# - C# loop parallel for
- jquery - Jquery Selectable 设置选中的类
- java - if 块如何删除重复字符?
- swift - 如何正确添加一行到 NSTableView
- python - 通过 ts3 api 发送 sendtextmessage
- html - 如何将固定元素放入转换后的容器中?
- c - C 凯撒密码函数调用未按预期运行