【问题标题】:Find whether I am authenticated or the XMLHttpRequest is forwarded查找我是否已通过身份验证或 XMLHttpRequest 是否已转发
【发布时间】:2016-09-17 16:57:26
【问题描述】:

我正在针对 API 执行 XMLHttpRequest。 API 通过针对单个提供商的 OAuth 身份验证进行保护,如果我从浏览器调用 API 并且我没有登录,它会自动将我转发到提供商的登录页面。

但如果我通过 Chrome 中的 XMLHttpRequest 从 Web 应用程序调用 API,我会得到:

DOMException: 无法在 'XMLHttpRequest' 上执行 'send': 无法加载 https://localhost/myapp/api/AppConfig

XMLHttpRequest 无法加载 https://login.microsoftonline.com/01234567-89ab-cdef-0123-4567890abcdef/oaut...。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问 Origin 'https://localhost'。

所以我想我必须找出我是否已登录,如果没有,请在新选项卡中打开登录窗口。

但是我怎样才能可靠地检测到我没有登录,并且 XMLHttpRequest 被转发 - 跨所有浏览器和所有 cors 设置?

try {
    xhr.send(null);
} catch(ex) {
    // how can I find that the issue is with authentication, and not something else?
}

【问题讨论】:

    标签: javascript ajax oauth oauth-2.0 cors


    【解决方案1】:

    可能不是答案;我与 Authorize.net 有类似的东西。可能会发生两件事:

    1. 您的防火墙正在阻止呼叫。

    2. 由于您使用null 发送呼叫,因此请求可能认为它是POST 请求并且需要标头:

    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    尝试删除null 并将send 留空。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2022-09-29
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      • 2015-02-15
      相关资源
      最近更新 更多