7

JSON.parse附带一个“reviver”函数(例如:“JSON.parse using reviver function”)。

如何将这个“复兴者”与response.json一起使用?例如:

fetch(url)
.then(a => a.json({reviver}))    // unfortunately not working
.then(...)

我有一个解决方法:

fetch(url)
.then(a => a.text())
.then(b => new Promise((resolve) => resolve(JSON.parse(b, reviver))))
.then()

但它使用了更多步骤,这似乎没用。有更好的主意吗?

4

2 回答 2

5

Your workaround is basically the best option but as mentioned, the extra promise is unnecessary.

fetch(url)
    .then(response => response.text())
    .then(text => JSON.parse(text, reviver))
    // ...
于 2019-10-19T12:32:58.900 回答
-4

你可以这样做:

fetch(url)
.then((response) => {return response.json()})
.then((json) => {console.log(json)});

希望能帮助到你 ;)

于 2019-10-19T11:33:27.640 回答