XML 来自 fetch 的响应

XML response from fetch

我在本机反应应用程序中使用 fetch。我正在尝试阅读响应,但是当我使用 alert(response) 时,我得到 [object Object]。我尝试过使用其他模块,例如 xml2js、react-native-html-parser' 或 xmldom.

fetch(
  /*
       REQUEST CONFIGURATION
  */
}).then(response => {
    console.log(response) // This is what returns [object Object]
  })

读取响应体为异步操作,需要先等待promise完成:

fetch(config)
  .then(response => response.text())
  .then(bodyText => {
    // now you have the response body you can parse
  });

另一方面,alert 是一个非常生硬的调试工具(我什至不知道它存在于 React Native 中!)。在 "Debug JS Remotely" 模式下尝试 console.log 以获得有关您要记录的内容的更多信息。

像这样处理响应:

.then(response => {
      response.text().then(text => {
        // handle response content
        })
      })