反应原生json响应无法解析
问题描述:
我刚刚从反应原生API获取数据。代码就像这样。反应原生json响应无法解析
fetch(Config.SERVER_URL + '/api/login/tokencheck/' + accessToken, {
method: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
})
.then((response) => response.text())
.then((responseJson) => {
alert(responseJson);
alert(JSON.stringify(responseJson));
})
.catch((error) => {
alert("error");
alert(error);
console.log(error);
})
的问题是,在在回调的第一个警报,它告诉我,有一个像这样{"name": "Abigail","state": "CA"}
JSON格式的字符串,但是,当我转换是respon使用JSON.parse
JSON它返回错误意外的标记“ ? 。但是当我主动反应原生Remote Debuger时,它工作得很好。任何解决方案
答
即使我之前遇到过这个问题。我认为这应该做到这一点。
componentDidMount() {
return fetch('http://example.com/getData')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
data: responseJson,
}, function() {
// do something with new state
});
})
.catch((error) => {
console.error(error);
});
}
我希望帮助:)
您可以使用您的数据是这样的。 ** ** this.state.data –