Ajax obtains the current server time

Source: Internet
Author: User

Use ajax to obtain the date value on the response header. Note the time zone. In the chrome development tool header, all values are Greenwich Mean Time, which is 8 hours less than Beijing time, the obtained time zone is related to the server settings. The following method is recommended:

// Obtain the current server time from the response header. The function getServerTime () {var xmlHttp = false; try {xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP ");} catch (e) {try {xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ") ;}catch (e2) {xmlHttp = false ;}} if (! XmlHttp & typeof XMLHttpRequest! = 'Undefined') {xmlHttp = new XMLHttpRequest ();} xmlHttp. open ("GET", window. location. href. toString (), false); xmlHttp. setRequestHeader ("Range", "bytes =-1"); xmlHttp. send (null); var severtime = new Date (xmlHttp. getResponseHeader ("Date"); return severtime}

In addition, there is a problem that the cache does not update the time through the ajax method of jquery.
Htmlobj = $. ajax ({url: "a.txt", async: false });
$ ("# MyDiv" ).html (htmlobj. responseText );
ResponseText: returned content
Async: false indicates that the Code after the return value is executed (synchronous thread)
Htmlobj. getResponseHeader ("Date ")
Obtain the time in the response header (Greenwich Mean Time, 8 hours slower than Beijing Time ),
When cache exists, the value in IE is null, and chrome time is not updated.
Firefox has frequent requests and there is a delay in time. The time will not be updated in a certain period of time (about one minute from the previous refresh ).


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.