Making a protocol agnostic jquery ajax call

2019-06-09 12:18发布

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条回答
劳资没心,怎么记你
2楼-- · 2019-06-09 12:36

Wouldn´t this work?

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

$.ajax({
    url : myUrl,
    ...
});
查看更多
登录 后发表回答