react-native请求问题

我需要请求一个这样的连接
http://news.sina.cn/gn/2017-0...
如果用fetch进行请求会报错JSON Parse error: Unrecognized token '<'
该如何进行请求,是否需要进行转换,用什么插件

阅读 3.6k
2 个回答

问题描述不够详细,相关的代码都不贴出来,别人怎么知道是不是你自己代码写的有问题。
我猜想很有可能是服务器返回的json格式数据的问题,返回的json字符串中含有隐藏的非法字符,可以这样解决

.then((response) => response.text())
.then((text) => {
if (Platform.OS === 'android') {
text = text.replace(/\r?\n/g, '').replace(/[\u0080-\uFFFF]/g, ''); // If android , I've removed unwanted chars.
}
return text;
})
.then(response=> {
ToastAndroid.show(JSON.parse(response).status, ToastAndroid.LONG)
})
.catch((error) => {
ToastAndroid.show(error.toString(), ToastAndroid.LONG)
console.warn(error);
});

JSON.parse就可以

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题