在react-native中执行formdata发布时总是抛出“网络错误”

时间:2019-06-17 03:00:04

标签: react-native

在我的本机应用程序中,我想通过api上传图像,它在邮递员中工作,但不在应用程序中,总是给我“网络错误” Postman image

  

TypeError:网络请求失败       在XMLHttpRequest.xhr.onerror(wwwg-fetch.js:504)       在XMLHttpRequest.dispatchEvent(event-target.js:172)       在XMLHttpRequest.setReadyState(XMLHttpRequest.js:580)       在XMLHttpRequest .__ didCompleteResponse(XMLHttpRequest.js:394)       在XMLHttpRequest.js:507       在RCTDeviceEventEmitter.emit(EventEmitter.js:181)       在MessageQueue .__ callFunction(MessageQueue.js:366)       在MessageQueue.js:106       在MessageQueue .__ guard(MessageQueue.js:314)       在MessageQueue.callFunctionReturnFlushedQueue(MessageQueue.js:105)

profile.js

@RunWith(AndroidJUnit4.class)
    public class PatientDaoTest {
    private static int newRowId;
    public static PatientRecordEntity newPatient1;


    public void generationRecord(){
        newRowId = 0;
        PatientRecordEntity newPatient1 = new PatientRecordEntity();
        newPatient1.setPatient_db_ID("23456");
        newPatient1.setPatient_race("Chines");
        newRowId = (int) patientDao.addNewPatient(newPatient1);
        newPatient1.setPid(newRowId);
    }

    @Test
    public void addNewPatient() throws Exception {
        boolean pin = false;

        if (0 != newRowId) {
        pin = true;
    }

    assertTrue("addNewPatient is not true", pin);
    }

2 个答案:

答案 0 :(得分:1)

尝试将标题写为

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

答案 1 :(得分:1)

您可以使用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);