在jQuery中获取默认服务器时间? [英] Getting default Server time in jQuery?

查看:168
本文介绍了在jQuery中获取默认服务器时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jQuery获取网络服务器时间?

How can I get the web-server time using jQuery?

推荐答案

这是一种在服务器端无需任何特定代码甚至没有有效路径的方法.

Here's a way that may work without any particular code, or even a valid path, on the server side.

try {
  var date = new Date($.ajax({'type': 'HEAD', 'url': '/'}).getResponseHeader('Date'));
}
catch(err) { 
  var date = null;
}

这是假定服务器将始终返回"Date"标头.必须对服务器的细节进行一些试验.

This is presumptive that the server will always return a "Date" header. Some experimentation with the particulars of your server will be necessary.

要添加:请注意,这更像是一种讨人喜欢的hacky方式. 正确"的方法是设置服务器端脚本以快速返回服务器时间,或者可以将其内联到将要运行脚本的页面上,或者简单地使用客户端时间(如果为足够好."

EDIT TO ADD: Note that this is more of a cutesy hackish way to do this. The "correct" way would be to set up a server-side script to return the server time quickly, or possibly put it inline to the page where the script will be running, or simply use the client-side time if it would be "good enough."

这篇关于在jQuery中获取默认服务器时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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