Xhttp request always returns a 0 response code. - While making a post request using form data

让人想犯罪 __ 提交于 2020-01-06 06:06:50

问题


I am trying to upload an array of images using form data and xhttp. Client : React Native - Both platforms Server : Node running on GCP Images go into s3.

Problem : Images are uploaded into s3. And when i look at my server logs i see the server is sending 200. But, client is always receiving response code 0. And also xhttp goes from state 1 -> 4.

We have added CORS on both ends so that might not be an issue. I have accept and content headers. But, no idea what's going wrong.

Any help is appreciated.

React-Native Client:

upload() {

let url = "MYAPIGOESHERE"

let uploadBody = new FormData()
let imagesUpload = []
imagesUpload.push({type:'image/jpg',uri:this.state.coverImage[0].uri,name:"cover"})

  uploadBody.append('photos',{uri:this.state.coverImage[0].uri, type:'image/jpg', name:"cover"})
  uploadBody.append('duration',this.state.duration)

  var xhttp = new XMLHttpRequest()

  xhttp.onreadystatechange = function() {

    console.log(this.readyState+" "+this.statusText)
      if (this.readyState === 4) {
        alert(this.status)  
        if (this.status === 200) {
          console.log(this.responseText);
        } else {
          console.log(this);
        }
      }
      if(this.readyState === 3)
      {
        console.log("Inside 3")
      }
      if(this.readyState === 2)
      {
        console.log("Inside 2")
      }
};


xhttp.open("POST", url)
xhttp.setRequestHeader('Accept', 'application/json');
xhttp.setRequestHeader('Access-Control-Allow-Origin', '*');
xhttp.responseType = 'json'
xhttp.send(uploadBody)

}

来源:https://stackoverflow.com/questions/58241300/xhttp-request-always-returns-a-0-response-code-while-making-a-post-request-us

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!