[英]jQuery check if call is cross-domain
想知道是否有任何方法可以检查该呼叫是否为跨域呼叫。 也许像标记这样的东西可以测试它是否是跨域的,所以我可以使用另一种方法。
例如:
$.ajax({
type: "POST",
url: "cross-domaincallURL",
success: function(response, status, xhr){
if OPTIONS then cross-domain
if not then not cross-domain
}
});
我想最简单的方法是将“ cross-domaincallURL”与window.location的域部分进行比较。
您可以将该逻辑封装到一个单独的函数中,或者将其添加到ajax对象中(但这是另一层)。
编辑:
您可以通过window.location.hostname https://developer.mozilla.org/en/DOM/window.location获取当前位置的域部分
我认为您可以在beforeSend(jqXHR,settings)事件中获取AJAX请求URL。 通过设置后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.