1

I have a very simple timeoftheserver.php page:

<?php
   echo date('D, d M y H:i:s');
?>

and on my local script is also simple:

var today;
try {
  today = new Date($.ajax({'type': 'HEAD', 'url': 'timeoftheserver.php'}).getResponseHeader('Date'));
}
catch(err) {
  today = new Date();
  alert("here");
}

alert(today);

but instead of the server time (or even the local time and the alert here) I got the popup:

Thu Jan 01 1970 01:00:00 GMT+0100 (Central Europe Standard Time)

What's wrong with this code?

jahller
  • 2,705
  • 1
  • 28
  • 30
randomuser1
  • 2,733
  • 6
  • 32
  • 68

1 Answers1

3

You're using a head request, which doesn't include the response body. Change "head" to "GET".

Otherwise, you could include the time in the headers and get it from there instead of sending it in the body.

Method 1) Using Headers:

Send the time as a header in PHP, change your PHP to:

header("x-app-date: ".date('D, d M y H:i:s'));

Then get the date in your AJAX like so...

$.ajax({
    'type': 'HEAD', 
    'url':'timeoftheserver.php',
    'complete': function(r){
         today = new Date(this.getResponseHeader('x-app-date'));
     }
});

Method 2: Using Body

Leave your PHP as is. Change your Ajax to:

$.ajax({
    'type': 'GET', 
    'url': 'timeoftheserver.php',
    'complete': function(resp){
        today = new Date(resp);
    }
});
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • Thanks, but when I changed the head to get nothing has changed, I'm still getting `Thu Jan 01 1970 01:00:00 GMT+0100 (Central Europe Standard Time)` . Can you give me some example of the 2nd advice? – randomuser1 Sep 29 '15 at 12:41
  • Instead of getting the response header you need to get the body of the response. – John Boker Sep 29 '15 at 12:45
  • Thanks for the clarification @John, could you give me some example how can I use it though? – randomuser1 Sep 29 '15 at 12:51
  • @randomuser1 take a peek at my edit.. please upvote/accept the answer if it helps. Good luck! – I wrestled a bear once. Sep 29 '15 at 12:56
  • @Adelphia thanks, I tried both approaches and in both cases I get the popup that says undefined, do you know what I might be doing wrong? – randomuser1 Sep 29 '15 at 13:03
  • @randomuser1 Yeah, I have an idea.. i think it has to do with the way you're defining `today`. You need to either move the alert into the `complete` function, or add an `async: false` parameter into your `$.ajax()` call. – I wrestled a bear once. Sep 29 '15 at 13:20
  • thanks, I basically want to use today later on quite a lot of times, so will the async:false solve my problem and let me use this value later on, not only in complete function? – randomuser1 Sep 29 '15 at 13:22
  • 1
    yep, so long as you define `today` in the global scope before making the ajax call. – I wrestled a bear once. Sep 29 '15 at 13:23
  • 1
    @randomuser1, though I should warn you, it's generally not a good idea to use synchronus ajax requests as it makes the browser hang until it's finished. It may be quite a bit of work to re-write your code to execute inside the callback, but really, that's the proper way to do it. Changing your async to false is really more of a quick n' dirty workaround. – I wrestled a bear once. Sep 29 '15 at 13:26
  • Ok, thank you very much, now I know almost everything :) The last thing that I want to know is when I do this: `complete: function(resp){ alert(resp);` I get the popup that says `Object object` and later on this line: `today = new Date(resp)` throws me an error `invalid date`. How can I parse it here? – randomuser1 Sep 29 '15 at 13:28
  • 1
    @randomuser1, okay, you're going to accept my answer after i spend all this time helping you out right? :p .. ok, if you're getting "object object" from the response, that means somewhere along the line, your jQuery is parsing the response from JSON. nothing in your code would explain why that's happening, so try manually parsing the response back into a string and let me know what happens. Add this as the first line in your `complete` function and let me know what happens. `resp = JSON.stringify(resp);` – I wrestled a bear once. Sep 29 '15 at 13:31
  • Of course :) When I put those lines in complete: `resp = JSON.stringify(resp); alert(resp); today = new Date(resp);` I get the popup that says `{"readyState":4,"responseText":"Tue, 29 Sep 15 15:51:03 pm","status":200,"statusText":"OK"}`... I think we're almost there, but how can I have the pure date on the alert? – randomuser1 Sep 29 '15 at 13:51
  • 1
    @randomuser1 oh, wierd. ok, so you're looking for the responseText property. so, just change `today = new Date(resp);` to `today = new Date(resp.responseText);`, and remove that `JSON.stringify()` line from earlier and you should be good to go :D – I wrestled a bear once. Sep 29 '15 at 14:09