如何在反应原生android中获取响应头?

时间:2016-06-29 08:23:06

标签: javascript android xmlhttprequest react-native response

您好我想在获取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();
    }, 




3 个答案:

答案 0 :(得分:4)

你可以这样做

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

答案 1 :(得分:2)

您可以从response.headers

中获取标题
fetch(URL_REGISTER, {
  method: 'POST',
  body: formData
})
  .then((response) => {
    console.log(response.headers); //Returns Headers{} object
}

答案 2 :(得分:0)

您应该像(return response.json();

一样返回response.json()