如何在 React Native android 中获取响应标头? [英] How to get response header in react native android?

查看:76
本文介绍了如何在 React Native android 中获取响应标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想在获取 POST 请求后获取响应标头.我尝试使用 console.log(response) 调试以查看 response 中的内容.我可以从 responseData 获取响应正文,但我不知道如何获取标头.我想同时获得标题和正文.请帮忙.谢谢:)

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:)

这是我所做的示例:

   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();
    }, 

推荐答案

你可以这样做

  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);
      });
}

阅读有关获取的更多信息:获取简介()

这篇关于如何在 React Native android 中获取响应标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆