Making a protocol agnostic jquery ajax call

2019-06-09 11:57发布

问题:

We have a widget that is embedded in various website. We would like the widget to make an ajax call to our server according to the protocol (http or https) of the website in which we are embedded. Is there a common practice to do that?

Using a protocol agnostic url (see below) doesn't work:

$.ajax({
    url : '//cdn.example.com/serviceName',
    ...
});

回答1:

Wouldn´t this work?

var myUrl = (window.location.protocol + '//cdn.example.com/serviceName');

$.ajax({
    url : myUrl,
    ...
});