如何在React Native Android中获取响应标头?

2 javascript android response xmlhttprequest react-native

您好,我想在获取POST请求后获取响应标头。我试图调试,看看里面有什么responseconsole.log(response)。我可以从中获取响应正文,responseData但我不知道如何获取标题。我想同时获得标头和正文。请帮忙。谢谢:)

这是我所做的示例:

   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();
    }, 
Run Code Online (Sandbox Code Playgroud)

Ash*_*k R 6

你可以这样

  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);
      });
}
Run Code Online (Sandbox Code Playgroud)

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