【问题标题】:XML response from fetch [duplicate]来自 fetch 的 XML 响应 [重复]
【发布时间】:2017-01-09 05:45:09
【问题描述】:

我在 react-native 应用程序中使用 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]
  })

【问题讨论】:

  • 这里是Response 文档:developer.mozilla.org/en-US/docs/Web/API/Response,尝试使用response.json()response.text()
  • 我做了,我收到了同样的结果
  • 如果你只是想看内容你应该使用console.log(response)并使用调试器查看对象。

标签: javascript xml react-native fetch


【解决方案1】:

像这样处理响应:

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-30
      • 2021-06-05
      • 2021-03-18
      • 2020-09-27
      • 1970-01-01
      • 2011-12-07
      • 2011-01-27
      相关资源
      最近更新 更多