开发者

jQuery check if call is cross-domain

开发者 https://www.devze.com 2023-02-20 07:27 出处:网络
Am wondering if there is any method to check if the call is a cros开发者_Python百科s-domain call. Maybe something like a flag which tests if it is cross-domain so then I can use another approach.

Am wondering if there is any method to check if the call is a cros开发者_Python百科s-domain call. Maybe something like a flag which tests if it is cross-domain so then I can use another approach.

For example:

$.ajax({
  type: "POST",
  url: "cross-domaincallURL", 
  success: function(response, status, xhr){ 
    if OPTIONS then cross-domain
    if not then not cross-domain 
  }
});


I suppose the easiest way would be to compare the "cross-domaincallURL" to the domain-part of window.location.

You could encapsulate that logic into a separate function or some how add it on to the ajax object (but that's another storey).

EDIT:

You can get the domain part of the current location via window.location.hostname https://developer.mozilla.org/en/DOM/window.location

I think you can get the AJAX requests URL in the beforeSend(jqXHR, settings) event. As it's passed the settings.

0

精彩评论

暂无评论...
验证码 换一张
取 消