首页 > 解决方案 > React Native,在使用 Axios 发布 FormData 时出现网络错误

问题描述

我正在尝试通过创建 FormData 来发送音频/图像。

我当前的版本是:“react-native”:“0.59.10”,“axios”:“^0.19.2”,“react”:“16.8.3”。我正在我的安卓设备上测试我的应用程序

并在发送 FormData 时每次都收到“网络错误”。

这是我创建和发送帖子的方式;

  let new_options = {
    headers: {
      'Content-Type': 'multipart/form-data',
      'Authorization': myAuthorization
    }
  };
  const content = new FormData();
  content.append('file', {
    uri: 'file://' + attachment.path,
    name: 'test',
    type:  attachment.type
  });
  content.append('ReportId', report_id);
  content.append('MimeType', attachment.type);
  
  try {
    let res = await axios.post(myUrl, content, new_options);
  }catch(err){
    console.warn(err);
  }

我已经尝试过更新 FLIPPER_VERSION 或编辑 ReactNativeFlipper.java 之类的东西,但是我的项目中不存在这些东西,因为我使用的是旧版本的 react-native。我还尝试删除调试文件夹。

标签: react-nativeaxiosreact-native-android

解决方案


想通了,问题是由错误的 MimeTypes 引起的。


推荐阅读