Ray Ray - 1 year ago 100
Ajax Question

How can I make an Ajax call to an HTTP time server site from HTTPS?

I'm trying to get the current time from http://timeapi.org/utc/now.json. I'm using the following:

var date;
dataType: 'jsonp',
url: 'http://timeapi.org/utc/now.json',
success: function (result) {
date = result.dateString;

The URL is only available as HTTP, but I'm calling it from an HTTPS site. This leads to the error:

'https://myurl.com' was loaded over HTTPS, but requested an insecure script 'http://timeapi.org/utc/now.json?callback=jQuery1122020058229618158618_1466258121249'. This request has been blocked; the content must be served over HTTPS.

The timeapi website does not actually exist as https, so changing the URL to https leads to a new error: http://timeapi.org/utc/now.json

How can I force it to load? There do not seem to be any https web time services, but I imagine there has to be a way in which people on https sites are using external time-keeping services as well.

Answer Source

You can't.

From HTTPS, only HTTPS. From HTTP you can call HTTP and HTTPS.

What you could do is to create an https wrapper (in php or node) in your own webserver and retrieve the value from timeapi.org from there.

Something like this:

header('Content-Type: application/json');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download