Fetch API 从响应中获取原始值
Fetch API get raw value from Response
我使用 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()
我看到response
是一个Response
对象,json
函数代码是return this.text().then(JSON.parse)
,我很困惑[的参数是什么? =16=]?那是 response
原始值吗?我怎样才能得到它?
这里是你如何做你想做的。在我的例子中,我想手动解析 JSON 因为内置的 JSON 解析器不正确地解析了某个字符 (\u001e)。
更改自:
fetch(url)
.then(response => response.json())
.then((data) => {
data....
至:
fetch(url)
.then(response => response.text())
.then((dataStr) => {
let data = JSON.parse(dataStr);
data...
我使用 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()
我看到response
是一个Response
对象,json
函数代码是return this.text().then(JSON.parse)
,我很困惑[的参数是什么? =16=]?那是 response
原始值吗?我怎样才能得到它?
这里是你如何做你想做的。在我的例子中,我想手动解析 JSON 因为内置的 JSON 解析器不正确地解析了某个字符 (\u001e)。
更改自:
fetch(url)
.then(response => response.json())
.then((data) => {
data....
至:
fetch(url)
.then(response => response.text())
.then((dataStr) => {
let data = JSON.parse(dataStr);
data...