如何在 React Native android 中获得响应 header?
How to get response header in react native android?
您好,我想在获取 POST 请求后得到响应 header。我尝试调试以查看 response
和 console.log(response)
中的内容。我可以从 responseData
获得响应主体,但我不知道如何获得 header。我想同时获得 header 和 body。请帮忙。谢谢:)
这是我所做的示例:
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();
},
您可以从 response.headers
中获取 headers
fetch(URL_REGISTER, {
method: 'POST',
body: formData
})
.then((response) => {
console.log(response.headers); //Returns Headers{} object
}
你可以这样做
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);
});
}
阅读有关获取的更多信息:Introduction to fetch()
你应该 return response.json() 喜欢
return response.json();
您好,我想在获取 POST 请求后得到响应 header。我尝试调试以查看 response
和 console.log(response)
中的内容。我可以从 responseData
获得响应主体,但我不知道如何获得 header。我想同时获得 header 和 body。请帮忙。谢谢:)
这是我所做的示例:
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();
},
您可以从 response.headers
fetch(URL_REGISTER, {
method: 'POST',
body: formData
})
.then((response) => {
console.log(response.headers); //Returns Headers{} object
}
你可以这样做
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);
});
}
阅读有关获取的更多信息:Introduction to fetch()
你应该 return response.json() 喜欢
return response.json();