react-native fetch - 请求正文 - 意外的 EOF

react-native fetch - request body - Unexpected EOF

在我的 react-native 应用程序中,我正在尝试使用 body 发出获取请求。但是,我收到 unexpected EOF 的错误消息。实际上,请求已发出,我的意思是我可以通过后端日志看到请求已发送,而在请求之后,它会显示错误消息。

这是我的fetch方法。

var Url = "https://----------";
        return fetch(Url, {
            method: "POST",
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({'number': '11111111-'})
        })
            .then((response) => response.json())
            .then((responseJson) => {
                console.log("SEND_SMS RESULT: ",responseJson);
            })
            .done();

这是我得到的错误屏幕。

我会说它在这一行失败了:response.json() 您确定您的回复有效吗JSON?

尝试使用 Postman 测试响应或在 done();

之前添加 .catch(e => console.log(e))
var Url = "https://----------";
    return fetch(Url, {
        method: "POST",
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({'number': '11111111-'})
    })
        .then((response) => response.text())
        .then((responseJson) => {
             const resposeJson2 = responseJson.length ? JSON.parse(responseJson) : {};
            console.log("SEND_SMS RESULT: ",responseJson2);
        })
        .done();

您的服务器正在返回 null 而不是错误,不幸的是 response.json() 无法对 null 响应进行操作 您可以对其进行简要研究,关键字是“处理来自 api”的空响应