javascript - 如何在 react native android中获取响应 header ?

标签 javascript android xmlhttprequest react-native response

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

这是我所做的示例:

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

阅读有关获取的更多信息:Introduction to fetch()

关于javascript - 如何在 react native android中获取响应 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38094103/

相关文章:

javascript - 是否有可能捕获 net::ERR_BLOCKED_BY_CLIENT?

javascript - 我们如何在 Angularjs 指令之外使用 $compile

android - Google Play,应用程序图标错误

安卓工作室 : how to properly use the support library AppCompat?

android - 对 PixelFormat 感到困惑

javascript - 播放 AudioBufferSourceNode 两次?

javascript - 引导模式在不应该打开时打开

javascript - 仅当文件上传完成时才会触发 XMLHttpRequest 事件 onProgress

javascript - 当我在 Internet Explorer 中执行打开的 xhhtprequest 时,权限被拒绝消息

javascript - 如何在我的机器上快速设置 "dummy"服务器? (电话间隙可访问)