问题
Hello i want to get response header after fetch POST request. I tried to debug to see what inside response
with console.log(response)
. I can get response bodies from responseData
but i have no idea how to get the header. I want to get both header and the body. Please help. thanks:)
Here's the example what i've done:
fetch(URL_REGISTER, {
method: 'POST',
body: formData
})
.then((response) => response.json())
.then((responseData) => {
if(responseData.success == 1){
this.setState({
message1: responseData.msg,
});
}
else{
this.setState({
message1: responseData.msg,
});
}
})
.done();
},
回答1:
you can do like this
fetchData() {
var URL_REGISTER = 'https://www.example.com';
fetch(URL_REGISTER, {method: 'POST',body: formData})
.then(
function(response) {
console.log(response.headers.get('Content-Type'));
console.log(response.headers.get('Date'));
console.log(response.status);
console.log(response.statusText);
console.log(response.type);
console.log(response.url);
if (response.status !== 200) {
console.log('Status Code: ' + response.status);
return;
}
// Examine the text in the response
response.json().then(function(data) {
console.log(data);
});
}
)
.catch(function(err) {
console.log('Fetch Error', err);
});
}
Read More about fetch: Introduction to fetch()
回答2:
You can take the headers from response.headers
fetch(URL_REGISTER, {
method: 'POST',
body: formData
})
.then((response) => {
console.log(response.headers); //Returns Headers{} object
}
回答3:
You should return response.json() like return response.json();
来源:https://stackoverflow.com/questions/38094103/how-to-get-response-header-in-react-native-android