当尝试通过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是我想要隔离的东西,所以我可以在其他地方链接到它。
答案 0 :(得分:1)
The browser is throwing the error and it is basically telling you that it is preventing you from accessing the resource. So try catching the exception and reading it from there.
EDIT: To clarify, the browser makes it seem to your code as if there was no response to the request. If there is no exception thrown then you are probably out of luck.