如何使用 AJAX 从另一台服务器获取时间? [英] How can I get time from another server using AJAX?

查看:15
本文介绍了如何使用 AJAX 从另一台服务器获取时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一些在线资源获取时间 - NTP 服务器、JSON 时间服务器,或者事件只是一个 HTTP 标头.我不在乎精度,但我确实需要从在线资源中获取时间.我无权访问任何服务器,因此无法编写任何服务器端代码.

I need to get time from some online source - NTP server, JSON time server, or maybe event just an HTTP Header. I don't care about precision, but I do need to get time from an online source. I don't have access to any servers so I can't write any server side code.

我无法使用 HTTP 请求来获取标头,因为我猜这会违反同源策略.

I can't do this using HTTP Request to get the header since I guess it would be violating the same origin policy.

而且我似乎找不到一种不涉及某种程度的 PHP 或其他语言的方法 - 我只能使用 HTML/CSS/JS...没有别的了!

And I can't seem to find a way of doing it that does not involve some level of PHP or other languages - I can only use HTML/CSS/JS...nothing else!

有什么想法吗?

推荐答案

$.ajax({
    url: 'http://timeapi.org/utc/now.json',
    dataType: 'jsonp'
})
.done(function(response) {
    // response = {"dateString":"2012-03-06T02:18:25+00:00"}
    console.log(response.dateString);// "2012-03-06T02:18:25+00:00"
});

这篇关于如何使用 AJAX 从另一台服务器获取时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆