【发布时间】:2015-04-28 16:36:58
【问题描述】:
当尝试通过 JQuerys AJAX 方法访问跨域站点时,Chrome 会抛出错误:
XMLHttpRequest cannot load http://somewebsite.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access
我了解 CORS 以及为什么会发生这种情况。我想要做的是将确切的错误消息保存到 javascript 变量中。我在具有该消息的响应请求错误中找不到任何内容,我认为它是 Chrome 处理 AJAX 请求的方式所独有的?
我问的原因是因为我试图隔离消息中的 URL,因为它为我提供了来自 AJAX 的正确重定向 URL。例如,如果我的 AJAX 调用命中“http://somewebsite.com/redirect”,则抛出的错误会显示实际重定向的 url,例如“http://somewebiste.com/trueURL”。真正的端点 URL 是我试图隔离的,因此我可以在其他地方链接到它。
【问题讨论】:
标签: jquery ajax redirect error-handling cors