Zer*_*how 9 promise react-native fetch-api
我使用 React-Native 请求一些数据,这是我的代码:
fetch('https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json')
.then((response)=>{
return response.json()
})
.then((responseJSON)=>{
callback(responseJSON)
})
.catch((error)=>{
console.error(error);
})
.done()
Run Code Online (Sandbox Code Playgroud)
我看到responseis 是一个Response对象,json函数代码是return this.text().then(JSON.parse),我很困惑JSON.parse?的参数是什么?这是response原始值吗?我怎么才能得到它?
And*_*ewL 12
这是你如何做你想做的。就我而言,我想手动解析 JSON,因为内置 JSON 解析器未正确解析某个字符 (\u001e)。
更改自:
fetch(url)
.then(response => response.json())
.then((data) => {
data....
Run Code Online (Sandbox Code Playgroud)
到:
fetch(url)
.then(response => response.text())
.then((dataStr) => {
let data = JSON.parse(dataStr);
data...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14160 次 |
| 最近记录: |