【问题标题】:React Native: set headers with the Linking apiReact Native:使用 Linking api 设置标题
【发布时间】:2017-01-05 05:13:52
【问题描述】:

如何在 React Native 中设置RequestHeader?像下面这样:

var client = new XMLHttpRequest();
client.open("GET", "http://www.example.com/api");
client.setRequestHeader("authorization", "Bearer Access_Token");
client.send();

我在 Linking api 文档中找不到任何信息。 https://facebook.github.io/react-native/docs/linking.html

【问题讨论】:

    标签: javascript reactjs react-native


    【解决方案1】:

    要向网站发出带有标头的请求,您需要 http(s) 请求,而不是在 2 个应用程序之间链接。

    正如其他答案中提到的,链接 api 没有标头。使用 http(s) 获取远程地址时确实有标头

    https://facebook.github.io/react-native/docs/network.html

    fetch('https://mywebsite.com/endpoint/', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        firstParam: 'yourValue',
        secondParam: 'yourOtherValue',
      })
    })
    

    【讨论】:

    • 但是如何将返回结果传输到浏览器中呢?
    • @deworde 检查包含链接,它有带有承诺链的样本和await'
    • 抱歉,不清楚。这解释了如何访问结果,但随后将该结果转发到 Safari 或 Chrome 是我正在努力解决的问题,openURL 会自动为您提供。
    • 这是题外话,请创建新问题
    • Done,但这是一个相关问题,我想看看它是否涵盖了这两个部分。
    【解决方案2】:

    Linking API 用于在设备上的应用程序之间进行链接。在应用程序之间链接时没有标题的概念。请改用 URL 中的查询参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      • 1970-01-01
      • 2016-10-11
      • 1970-01-01
      • 2019-01-22
      相关资源
      最近更新 更多