首页 > 解决方案 > 如何修复 ReactNative 中发送的“Json Parse Error: Unexpected EOF”错误?

问题描述

所以我正在开发一个 ReactNative 应用程序,并创建一个新用户,我正在使用用户提供的信息创建一个 Json,并将其发送到一个 PhP 文件,该文件解析 Json 接收到的将其存储在数据库中。但问题是我的 JSON 没有发送,而且我有这个宏伟的红屏“JSON 解析错误:意外 EOF”

这是我的 Fetch 代码:

   Insert_Data_Into_MySQL = () =>
{
    this.setState({ ActivityIndicator_Loading : true }, () =>
    {
        fetch('http://MyPathToMyServerButDontWorryItsAnExample/NewUser.php',
        {
            method: 'POST',
            headers: 
            {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(
            {
              product_name : this.state.Product_Name,
              product_number : this.state.Product_Number,
              product_details : this.state.Product_Details
            })

        })
        .then((response) => response.json()).then((responseJsonFromServer) =>
        {
            Alert.alert(responseJsonFromServer);
            this.setState({ ActivityIndicator_Loading : false });
        }).catch((error) =>
        {
            console.error(error);
            this.setState({ ActivityIndicator_Loading : false});
        });
    });
}

非常感谢你们!

标签: jsonapireact-nativefetch

解决方案


推荐阅读