JavaScript is run on the client's script, we generally use the new date () to get the current time, but the time is the client, the client time is arbitrary change, if you want to do a product launch countdown, the client time a change, it will be fool. There are many scenarios in the business that require server time, so how can I get the server time by JS only? In fact, only one Ajax request is needed to get the current server time by reading the timestamp inside the XMLHttpRequest object's request header !
The principle is so simple: through Ajax to the server to send a request, when the server receives the request can read the request header timestamp, regardless of the success or failure of the request, you can get the timestamp. How to tell the server received the request? When the AJAX request is sent, the XMLHttpRequest has a status change of 5:
Xmlhttprequest.readystate value |
The meaning of the expression |
0 |
Uninitialized, a XMLHttpRequest object has been created , but has not yet been initialized |
1 |
not sent, the code has called the XMLHttpRequest Open () method and XMLHttpRequest is ready to send a request to the server |
2 |
has been sent, a request has been sent to the server by means of the Send () method, but no response has been received that can read the request header information |
3 |
is receiving, HTTP response header information has been received, but the message body part has not been fully received |
4 |
is loaded, The response has been fully received |
By listening to XMLHttpRequest's ReadyStateChange event to determine which state is currently in, it can be seen from the table that when A xmlhttprequest.readystate value of 2 o'clock can read the request header to get the time stamp we want. The code is as follows:
<p id= "Time" ></p><script>Ajax ()functionAjax (option) {varXHR =NULL; if(window. XMLHttpRequest) {XHR=Newwindow. XMLHttpRequest (); }Else{//IEXHR =NewActiveobject ("Microsoft") } //request the current file by GetXhr.open ("Get", "/"); Xhr.send (NULL); //Monitoring Request status changesXhr.onreadystatechange =function(){ varTime =NULL, Curdate=NULL; if(xhr.readystate===2){ //gets the timestamp of the request headerTime = Xhr.getresponseheader ("Date"); Console.log (Xhr.getallresponseheaders ()) Curdate=NewDate (time); document.getElementById (InnerHTML = "Server time is:" +curdate.getfullyear () + "-" + (Curdate.getmonth () +1) + "-" +curdate.getdate () + "" + Curdate.gethours () + ":" +curdate.getminutes () + ":" +curdate.getseconds (); } } }</script>
In this, the server time is taken. In fact, the information in the request header is not only timestamp, can use Xhr.getallresponseheaders () to obtain the entire request header information, the request header also includes the server type and version number, the requested file type and encoding and other information (see a), can be read in this way. In order to prove that the acquisition is really server time, I used a phone test (see II), the current Beijing time is April 20, 2015 21:59,PC machine time I made it to February 1, 2015 21:16
Figure A
Figure II
This address: http://www.cnblogs.com/wangmeijian/p/4442830.html
Reprint please keep the source!
JavaScript gets server time