0

I am making ajax request to a particular server and getting the response also. I am doing cross domain request so using jsonp. Is there a way to get the server time to which i am making the request. Will it be better to write php script or doing just ajax request is good. Suppose if i make the following request :

$.ajax({
        dataType: 'jsonp',
        data: 'jsonp=date',
        jsonp: 'jsonp_callback',                
        url: 'http://www.google.com',   
        success: function (data) {                  
        }
});

How can i get the server time from this request? Please help if any suggestion. Also after getting the time if i use setInterval method to update time every second will it be a costly operation or better to make the same ajax request after a particular time to update time. I have real time data to update with the time.

4

3 回答 3

2

You can't get the server's time if it doesn't explicitly provide it to you.
You can read its HTTP headers, but that's not a good thing since the headers may not provide this information every time, or their format may not be the same all the time.

Also, I don't see the point of asking it every second.
Ask it one single time, calculate the difference between its time and yours, and here you go: you got the difference of time and you can use it wherever you want.

Keep in mind that even if you get the time, there probably will be a difference between the one you got and the server's real time because of the network's latency.

于 2012-05-21T18:53:29.810 回答
1

The answer to part 1 is that you will need to output the server time in the response in order for your javascript to read it. For part 2, I would wait for the response to load, and then use setTimeout. Using setInterval means that you might fire the ajax call twice before the first response returns.

于 2012-05-21T18:51:47.877 回答
0

When you suggest server time do you mean the script that is executing the call or the actual time on the server?

What is the need for this, are you trying to figure out time between calls to automate calls every 5 secs for example?

If you are then just simply getting a locale time from javascript and comparing that from when you get a response in ajax would suffice?

Otherwise javascript isn't going to be able to get server time, due to its 'clientside' nature.

于 2012-05-21T18:57:35.847 回答