首页 > 解决方案 > .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;
    })
})

标签: javascriptpromisefetchfetch-api

解决方案


.then((resolve) => resolve.json())

api返回响应,fetch但是为了解析json响应,需要调用json函数。这也返回了一个最终返回 json 数据的 promise。

因此,这一行正在解析 json。


推荐阅读