问题
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