【问题标题】:Axios network error even though the post request returns 200即使post请求返回200,axios网络错误
【发布时间】:2020-09-14 17:53:39
【问题描述】:

在我的服务器上启用 CORS 和所有内容后,错误仍然存​​在。

在我的应用程序内的其他形式中,上传图片工作......但在这种确切的形式中,在 iPhone 上它工作得非常好,但在提交后在 android 上,我得到的只是一个“网络错误”,尽管帖子返回 200。我认为这是一个 axios 问题。 只有在 android 上我才会遇到这个问题。

我的代码如下:

           const data = new FormData()
            data.append('subject_id', this.props.navigation.getParam('id'))
            data.append('name', this.state.title)
            data.append('progress', this.state.progress * 100)
            data.append('description', this.state.description)
            data.append('date', this.state.date)
            data.append('image', {
                uri: this.state.image,
                type: 'image/jpeg',
                name: 'image'
            });

            axios.post('https://example.com/api/auth/createTask', data, {
                headers: {
                    'Authorization': access,
                    "Content-Type": "multipart/form-data"
                },
            }).then(res => {
                this.props.navigation.navigate('ViewHW', { id: res.data.id })
            }).catch(res => {
                console.log(res)
            })

非常感谢您对此的帮助。

【问题讨论】:

标签: react-native axios


【解决方案1】:

我怀疑这是 axios 的问题。

如果您使用的是图像选择器或相机,请务必查看文档,因为所选文件的路径在 android 和 iOS 之间有所不同。

确保根据 platform.OS === 'android' 更改项目的路径。 无论您使用什么,都应该在文档中清楚地描述它。

【讨论】:

  • 不完全是答案。我在我的应用程序的其他部分使用该表单,它运行良好
【解决方案2】:

这最终成为 react-native 的一个问题。该错误现已修复(新版本 0.63.3)

【讨论】:

    猜你喜欢
    • 2018-10-23
    • 2019-05-06
    • 2019-04-02
    • 1970-01-01
    • 2020-10-26
    • 2022-01-05
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    相关资源
    最近更新 更多