Reactjs 使用FormData()HTTP TypeError响应本机获取()错误:网络请求失败

Reactjs 使用FormData()HTTP TypeError响应本机获取()错误:网络请求失败,reactjs,react-native,Reactjs,React Native,在安卓系统上添加图片后,我使用FormData()和Fetch()将文件发布到我的服务器并将其镜像 我使用的是公共IP,没有发布到我的本地主机或任何东西,也没有使用HTTPS,还使用Genymotion Emulator 我的代码: var fd = new FormData(); fd.append('image', {path:'file:///'}); fetch(UPLOAD_URI,{ method: 'POST',

在安卓系统上添加图片后,我使用FormData()Fetch()将文件发布到我的服务器并将其镜像

我使用的是公共IP,没有发布到我的本地主机或任何东西,也没有使用HTTPS,还使用Genymotion Emulator

我的代码:

      var fd = new FormData();
      fd.append('image', {path:'file:///'}); 

      fetch(UPLOAD_URI,{
        method: 'POST',
        headers: {
          'Content-Type': 'multipart/form-data',
        },
        body: fd
        }).then(response => {
          console.log("image uploaded")
        }).catch(err => {
          console.log(err)
        })
TypeError: Network request failed
    at XMLHttpRequest.xhr.onerror (fetch.js:441)
    at XMLHttpRequest.dispatchEvent (event-target.js:172)
    at XMLHttpRequest.setReadyState (XMLHttpRequest.js:542)
    at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:378)
    at XMLHttpRequest.js:482
    at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
    at MessageQueue.__callFunction (MessageQueue.js:236)
    at MessageQueue.js:108
    at guard (MessageQueue.js:46)
    at MessageQueue.callFunctionReturnFlushedQueue (MessageQueue.js:107)
var FileTransfer = require('react-native-file-transfer-android');

const UPLOAD_URI = "http://< IP / Domain>/index.php"


  makeUuid()
  {
      var text = "";
      var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

      for( var i=0; i < 5; i++ )
          text += possible.charAt(Math.floor(Math.random() * possible.length));

      return text;
  }

  sendImageToServer(data){

        var uuid = this.makeUuid();
        FileTransfer.upload({
          uri: data,
          uploadUrl: UPLOAD_URI,
          fileName: 'temp.jpg',
          mimeType: 'image/jpg',
          headers: {
            'Accept': 'application/json'
          },
          data: {
            name:uuid
          }
            }, (err, res) => {
              if(err) {
                console.error(err);
              } else {
                console.log(res);
              }
          });
   }

  saveImage(imgDate){

    let states = {camera:false};
    states[this.state.ActiveImage] = imgDate.path;
    this.sendImageToServer(imgDate.path);
    this.setState({
      ...states
    });




  }
捕获中作为参数err返回的错误:

      var fd = new FormData();
      fd.append('image', {path:'file:///'}); 

      fetch(UPLOAD_URI,{
        method: 'POST',
        headers: {
          'Content-Type': 'multipart/form-data',
        },
        body: fd
        }).then(response => {
          console.log("image uploaded")
        }).catch(err => {
          console.log(err)
        })
TypeError: Network request failed
    at XMLHttpRequest.xhr.onerror (fetch.js:441)
    at XMLHttpRequest.dispatchEvent (event-target.js:172)
    at XMLHttpRequest.setReadyState (XMLHttpRequest.js:542)
    at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:378)
    at XMLHttpRequest.js:482
    at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
    at MessageQueue.__callFunction (MessageQueue.js:236)
    at MessageQueue.js:108
    at guard (MessageQueue.js:46)
    at MessageQueue.callFunctionReturnFlushedQueue (MessageQueue.js:107)
var FileTransfer = require('react-native-file-transfer-android');

const UPLOAD_URI = "http://< IP / Domain>/index.php"


  makeUuid()
  {
      var text = "";
      var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

      for( var i=0; i < 5; i++ )
          text += possible.charAt(Math.floor(Math.random() * possible.length));

      return text;
  }

  sendImageToServer(data){

        var uuid = this.makeUuid();
        FileTransfer.upload({
          uri: data,
          uploadUrl: UPLOAD_URI,
          fileName: 'temp.jpg',
          mimeType: 'image/jpg',
          headers: {
            'Accept': 'application/json'
          },
          data: {
            name:uuid
          }
            }, (err, res) => {
              if(err) {
                console.error(err);
              } else {
                console.log(res);
              }
          });
   }

  saveImage(imgDate){

    let states = {camera:false};
    states[this.state.ActiveImage] = imgDate.path;
    this.sendImageToServer(imgDate.path);
    this.setState({
      ...states
    });




  }
是否缺少某种权限,有什么问题


干杯,

对于我在android上的案例,以下模块帮助我解决了这个问题,因为它使用java将我的文件发送到服务器,并使用PHP中的$\u文件获取:

npm install react-native-file-transfer-android
我的代码:

      var fd = new FormData();
      fd.append('image', {path:'file:///'}); 

      fetch(UPLOAD_URI,{
        method: 'POST',
        headers: {
          'Content-Type': 'multipart/form-data',
        },
        body: fd
        }).then(response => {
          console.log("image uploaded")
        }).catch(err => {
          console.log(err)
        })
TypeError: Network request failed
    at XMLHttpRequest.xhr.onerror (fetch.js:441)
    at XMLHttpRequest.dispatchEvent (event-target.js:172)
    at XMLHttpRequest.setReadyState (XMLHttpRequest.js:542)
    at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:378)
    at XMLHttpRequest.js:482
    at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
    at MessageQueue.__callFunction (MessageQueue.js:236)
    at MessageQueue.js:108
    at guard (MessageQueue.js:46)
    at MessageQueue.callFunctionReturnFlushedQueue (MessageQueue.js:107)
var FileTransfer = require('react-native-file-transfer-android');

const UPLOAD_URI = "http://< IP / Domain>/index.php"


  makeUuid()
  {
      var text = "";
      var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

      for( var i=0; i < 5; i++ )
          text += possible.charAt(Math.floor(Math.random() * possible.length));

      return text;
  }

  sendImageToServer(data){

        var uuid = this.makeUuid();
        FileTransfer.upload({
          uri: data,
          uploadUrl: UPLOAD_URI,
          fileName: 'temp.jpg',
          mimeType: 'image/jpg',
          headers: {
            'Accept': 'application/json'
          },
          data: {
            name:uuid
          }
            }, (err, res) => {
              if(err) {
                console.error(err);
              } else {
                console.log(res);
              }
          });
   }

  saveImage(imgDate){

    let states = {camera:false};
    states[this.state.ActiveImage] = imgDate.path;
    this.sendImageToServer(imgDate.path);
    this.setState({
      ...states
    });




  }
var FileTransfer=require('react-native-file-transfer-android');
const UPLOAD_URI=“http:///index.php”
makeUuid()
{
var text=“”;
var-mablue=“ABCDEFGHIJKLMNOPQRSTUVXYZABCDFGHIJKLMNOPQRSTUVXYZ0123456789”;
对于(变量i=0;i<5;i++)
text+=可能的.charAt(Math.floor(Math.random()*可能的.length));
返回文本;
}
sendImageToServer(数据){
var uuid=this.makeUuid();
FileTransfer.upload({
uri:数据,
uploadUrl:UPLOAD_URI,
文件名:“temp.jpg”,
mimeType:'image/jpg',
标题:{
“接受”:“应用程序/json”
},
数据:{
名称:uuid
}
},(误差、分辨率)=>{
如果(错误){
控制台错误(err);
}否则{
控制台日志(res);
}
});
}
保存图像(imgDate){
让状态={camera:false};
states[this.state.ActiveImage]=imgDate.path;
this.sendImageToServer(imgDate.path);
这是我的国家({
…国家
});
}

干杯。

可能的重复是使用HTTPS,而不使用HTTPS。您的uri是HTTPS吗?