【发布时间】: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