如何从HTTPS对HTTP时间服务器站点进行Ajax调用?

Ray*_*Ray 3 javascript ajax https

我正在尝试从http://timeapi.org/utc/now.json获取当前时间。我正在使用以下内容:

  var date;
  $.ajax({
    dataType: 'jsonp',
    url: 'http://timeapi.org/utc/now.json',
    success: function (result) {
        date = result.dateString;
    }
  });
Run Code Online (Sandbox Code Playgroud)

该URL仅可作为HTTP使用,但我是从HTTPS站点调用它。这导致错误:

https://myurl.com ”是通过HTTPS加载的,但请求了不安全的脚本“ http://timeapi.org/utc/now.json?callback=jQuery1122020058229618158618_1466258121249 ”。该请求已被阻止;内容必须通过HTTPS提供。

timeapi网站实际上不以https形式存在,因此将URL更改为https会导致一个新错误:http : //timeapi.org/utc/now.json

如何强制加载?似乎没有任何HTTP Web时间服务,但我想必须有一种方式可以使https站点上的人们也使用外部计时服务。

Ser*_*ura 5

你不能

从HTTPS,只有HTTPS。通过HTTP,您可以调用HTTP和HTTPS。

您可以做的是在自己的网络服务器中创建一个https包装器(在php或节点中),然后从timeapi.org中检索值。

像这样:

<?php
header('Content-Type: application/json');
echo(file_get_contents('http://timeapi.org/utc/now.json'));
Run Code Online (Sandbox Code Playgroud)