简体   繁体   English

React Native获取有时会捕获JSON解析错误

[英]React Native fetch sometimes catch a JSON parse error

I'm working on a React Native application, which fetches an API response from a server. 我正在开发一个React Native应用程序,该应用程序从服务器获取API响应。 Sometimes, it returns success, but sometimes it catches an error. 有时,它返回成功,但有时会捕获错误。

The following code is my fetch function: 以下代码是我的提取函数:

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
    });
};

It was returning various error messages, eg: 它返回各种错误消息,例如:

[SyntaxError: JSON Parse error: "\\u302\\" is not a valid unicode escape] [SyntaxError:JSON解析错误:“ \\ u302 \\”不是有效的Unicode转义]
[SyntaxError: JSON Parse error: Invalid escape character 4] [SyntaxError:JSON解析错误:无效的转义字符4]
[SyntaxError: JSON Parse error: "\\u740\\" is not a valid unicode escape] [SyntaxError:JSON解析错误:“ \\ u740 \\”不是有效的Unicode转义]

I'm already checked my API response through the browser and it didn't find anything wrong with the response. 我已经通过浏览器检查了我的API响应,但响应没有发现任何问题。 I thought there was something wrong with my ES code maybe. 我以为我的ES代码可能有问题。

What is going wrong here...? 这是怎么回事...?

The \\u302\\ character appears to be something similar to the ^ symbol called a "COMBINING CIRCUMFLEX ACCENT" http://www.fileformat.info/info/unicode/char/0302/index.htm \\ u302 \\字符似乎与称为“ COMBINING CIRCUMFLEX ACCENT”的^符号类似, http://www.fileformat.info/info/unicode/char/0302/index.htm

And \\u740\\ is a "SYRIAC FEMININE DOT http://www.fileformat.info/info/unicode/char/0740/index.htm \\ u740 \\是一个“叙利亚女性点http://www.fileformat.info/info/unicode/char/0740/index.htm

The JSON parser just isn't able to handle these characters I guess, or at least not in the way they're presented. 我猜JSON解析器无法处理这些字符,或者至少不能处理它们的呈现方式。 It looks like they're being used as an escape character, but it's hard to know since you didn't provide samples of the output that generated each error. 看起来它们被用作转义字符,但是很难知道,因为您没有提供生成每个错误的输出样本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM