Pergunta

How to do? I am a beginner coder - full references and code would help. I literally spent like 5 hours trying to find a solution to this - there are some references online but nothing works! And I don't have access to the NTP server and yes I have to use a public server - such as time.nist.gov.

Help!!!

Foi útil?

Solução

Short answer: Not doable.

Long answer: Even if cross-origin policy would allow it, there's no way to get NTP directly via Ajax without PHP (or something) relaying your request. First reason is that time servers normally stay on UDP port 123; there's no way for Ajax to do UDP; if that's not enough, when Ajax sends a request to a server, it expects to see in response some HTTP headers, some status codes, a response body, etc. NTP doesn't keep that structure, it only sends a string. And there is no raw socket connection support in HTML5 either.

But what you can do with Ajax is look at the request headers because most responses come back with a header that looks like this:

 Date:Mon, 21 May 2012 15:30:58 GMT

And there's your time.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top