【发布时间】:2018-03-31 00:48:42
【问题描述】:
我正在开发一个 React Native 应用程序,它从服务器获取 API 响应。有时,它返回成功,但有时它会捕获错误。
以下代码是我的fetch函数:
export default async (url, body = null, method = 'GET') => {
let config = {
method,
};
return await fetch(url, config).then((response) => {
if (!response.ok) {
throw Error(response.statusText);
}
return response.json();
}).catch(error => {
console.warn(error); // sometimes it was catched in here
});
};
它正在返回各种错误消息,例如:
[SyntaxError: JSON Parse error: "\u302\" 不是有效的 unicode 转义]
[SyntaxError:JSON 解析错误:无效的转义字符 4]
[SyntaxError: JSON Parse error: "\u740\" is not a valid unicode escape]
我已经通过浏览器检查了我的 API 响应,它没有发现响应有任何问题。我认为我的 ES 代码可能有问题。
这里出了什么问题...?
【问题讨论】:
标签: javascript reactjs react-native fetch