提问者:小点点

在react-native中进行formdata发布时始终抛出“网络错误”


TypeError:在XMLHttpRequest. setReadyState(XMLHttpRequest.js:580)的XMLHttpRequest.xhr.onerror(whwg-fetch.js:504)的XMLHttpRequest.调度事件(event-target.js:172)的XMLHttpRequest.setReadyState(XMLHttpRequest.js:580)的XMLHttpRequest.xhr.onerror(whwg-fetch.js:504)的XMLHttpRequest.xhr.onerror(whwg-fetch.js:504)在XMLHttpRequest.setReadyState(XMLHttpRequest.js:580)在XMLHttpRequest.js:507在RCTDeviceEventEventEmitter.emit(EventEmitter.js:181)在MessageQueue.__callFunction(MessageQueu.js:366)在MessageQueue

配置文件. js

let formData = new FormData();
 formData.append("file", {
      name: response.fileName,
      uri: response.uri,
      type: response.type
    });

    formData.append("inputString", {
      appversion: "2.0.0",
      mobile: "456456456",
      token: "1ba12dyrty452c4d6cb04544f52fccfc92748f"
    });

    var xhr = new XMLHttpRequest();

    xhr.upload.onprogress = function(e) {
      var percentComplete = Math.ceil((e.loaded / e.total) * 100);
      // here you will get the percentage of upload completed.
    };

    xhr.open("POST", "https://findmysalon.appspot.com/customer/uploadfile");

    xhr.onload = function() {
      if (xhr.status >= 200 && xhr.status < 300) {
        let resp = xhr.response;
        var response = JSON.parse(resp);
        console.log(response);
      } else {
        console.log(xhr.response);
        console.log("error1");
        //Response error
      }
    };

    xhr.onerror = function() {
      //ERROR
      console.log("error2");
    };

    xhr.setRequestHeader("Authorization", "Basic dfgdfgdgddfg"); //eg: `Bearer ${token}`
    xhr.send(formData);

共2个答案

匿名用户

您可以使用XMLHttpRequest()

例如:

let formData = new FormData();
let fileName = "image1.png";

formData.append("file", {
  name: fileName,
  uri: img.uri,
  type: "image/png"
});

form.append("inputString", {
  appversion: this.state.appversion,
  mobile: this.state.mobile,
  token: this.state.token
});

var xhr = new XMLHttpRequest();

xhr.upload.onprogress = function(e) {
  var percentComplete = Math.ceil((e.loaded / e.total) * 100);
  // here you will get the percentage of upload completed.
};

xhr.open("POST", "https://fhy.appspot.com/customer/uploadfile");

xhr.onload = function() {
  if (xhr.status >= 200 && xhr.status < 300) {
    let resp = xhr.response;
    var response = JSON.parse(resp);
    //Response success
  } else {
    //Response error
  }
};

xhr.onerror = function() {
  //ERROR
};

xhr.setRequestHeader("Authorization", "dfg dfgdfgdfgdfgdfg"); //eg: `Bearer ${token}`
xhr.send(formData);

匿名用户

尝试将标题写入

  headers: {
    "Authorization", "dfg dfgdfgdfgdfgdfg";
    "content-type", "multipart/form-data";
  },