简体   繁体   English

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

[英]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 . 我正在尝试从http://timeapi.org/utc/now.json获取当前时间。 I'm using the following: 我正在使用以下内容:

  var date;
  $.ajax({
    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. 该URL仅可作为HTTP使用,但我是从HTTPS站点调用它。 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 '. https://myurl.com ”是通过HTTPS加载的,但请求了不安全的脚本“ http://timeapi.org/utc/now.json?callback=jQuery1122020058229618158618_1466258121249 ”。 This request has been blocked; 该请求已被阻止; the content must be served over HTTPS. 内容必须通过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 timeapi网站实际上不以https形式存在,因此将URL更改为https会导致一个新错误: 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. 似乎没有任何HTTP Web时间服务,但我想必须有一种方式可以使https站点上的人们也使用外部计时服务。

You can't. 你不能

From HTTPS, only HTTPS. 从HTTPS,只有HTTPS。 From HTTP you can call HTTP and HTTPS. 通过HTTP,您可以调用HTTP和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. 您可以做的是在自己的网络服务器中创建一个https包装器(在php或节点中),然后从timeapi.org中检索值。

Something like this: 像这样:

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

You can't make http requests from an https. 您无法通过https发出http请求。

It is restricted by same origin policy 同一原产地政策限制

The same-origin policy is a critical security mechanism that restricts how a document or script loaded from one origin can interact with a resource from another origin. 同源策略是一种重要的安全机制,它限制了从一个来源加载的文档或脚本如何与另一个来源的资源进行交互。 It helps isolate potentially malicious documents, reducing possible attack vectors. 它有助于隔离潜在的恶意文档,从而减少可能的攻击媒介。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM