【问题标题】:How can I log an HTTP POST response in React Native app?如何在 React Native 应用程序中记录 HTTP POST 响应?
【发布时间】:2018-01-27 03:58:37
【问题描述】:

我有一个带有按钮的简单 React Native 应用程序。在按下时,它会调用以下发出 POST 请求的函数。 我已经在 POSTMAN 中进行了测试,并且请求有效。但是,我想测试我的应用程序,但无法记录响应或任何错误。只有 'here' 和 'done' 的 console.log 语句正在打印(我在 Chrome 中使用远程调试器看到它​​们)。

似乎 fetch 函数被忽略了,没有警告/错误/日志。 我在这里做错了什么?或者确保请求有效的最佳方法是什么?

_onPressButton() {
  console.log('here');
  fetch('http://passport.local/oauth/token', {  
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      client_id: '2',
      client_secret: 'value',
      grant_type: 'password',
      username: 'value',
      password: 'value',
    })
  })
  .then((response) => response.json())
  .then((responseJson) => {
    console.warn(xhr.responseText);
    console.log(responseJson);
    return responseJson;
  })
  .catch((error) => {
    console.error(error);
  });

  console.log('done');
}

【问题讨论】:

    标签: post logging react-native fetch


    【解决方案1】:

    好像虚拟主机没有被解析。我使用 ngrok 将地址“passport.local”隧道传输到 ngrok URL 并且它有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 2017-10-08
      • 2021-01-03
      • 1970-01-01
      相关资源
      最近更新 更多