【问题标题】:Cordova app doesn't have access to HTTP response headersCordova 应用程序无权访问 HTTP 响应标头
【发布时间】:2021-03-31 15:46:22
【问题描述】:

我正在将 React 16 应用程序迁移到 Apache Cordova (v10.0)。我正在使用axios HTTP 库。

当我运行原始版本时,我的应用程序可以使用响应对象中的不记名令牌(等)访问身份验证响应标头。但是当我运行相同的应用程序并从 Cordova 发出相同的请求时,我无法获得身份验证令牌。当我console.log 响应对象时,Cordova 版本只接收两个标头:cache-controlcontent-type

import axios from 'axios';

const myApi = axios.create({
    withCredentials: false,
    headers: {
        'Accept': 'application/json',
        'Cache-Control': 'no-cache'
    }
});

    myApi.post(authenticationPath, {
      email: 'foo@example.com',
      password: 'bar'
    })
    .then( response => {
      console.log(response);
      // In the original version, response.headers has auth token, etc
      // In the Cordova version, response.headers only has cache-control + content-type
    })

更新:我检查了开发工具中的原始 HTTP 响应,并且那里缺少的标头 。所以看起来我的后端 API 响应正确,但由于某种原因,javascript 在响应对象中看不到它们(在我的应用程序的 Cordova 版本中)

【问题讨论】:

    标签: reactjs cordova axios phonegap


    【解决方案1】:

    我在这里找到了我的问题的解决方案:Axios get access to response header fields

    TLDR = 对于 CORS 响应,浏览器只能看到几个“安全”标头。要查看其余内容,服务器需要使用额外的标头显式地将它们列入白名单。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      • 1970-01-01
      相关资源
      最近更新 更多