El método jquery $ .get get no funciona [cerrado]

          <script type="text/javascript" src="jquery.js"></script>
          <script type="text/javascript">
               var currenttime;
               function getDateInfo() { 
                 $.get("time.php?a=" + Math.random() , function(data) {
                   return data;


                currenttime = getDateInfo();
/**************file time.php contains following code************/
    echo "August 27, 2011 19:30:52";

Hello friends , please help why this code is not working..

preguntado el 28 de agosto de 11 a las 02:08

What are you trying to do? What errors have your received? -

That isn't how it works. -

To the downvoters: True, the OP could have asked it better; but if you take a moment to read the code, it is clear what the problem is - and it is an interesting problem by the way. -

@MajidFouladpour the question as it exists now is unlikely to help anyone else in the future. Care to edit it? -

2 Respuestas

Al get call is asynchronous. It returns to your code as soon as it has asked the browser to Comienze aquí the remote request. Your code then displays an alert without waiting for the request to complete -- so at that time there is of course no result yet.

This is why the function takes a callback argument instead of just returning the result. Your callback will be run long after getDateInfo() has returned, and you must arrange things such that actions that depend on the answer are started by the callback function rather than by the code that calls $.get.

Respondido 28 ago 11, 06:08

Are you sure? I replaced alert(currenttime); con setTimeout("alert(currenttime);",3000); and the ajax call is taking 50 to 100 ms (so alert is fired after the result has arrived), but still the alert spits undefined. ¿Por qué? - Majid Fouladpour

That's because the result is never stored in currenttime. What get stored in currenttime is the result value from getDateInfo, which is undefined because it has no return declaración. La return within the callback function returns the server's reply to the code inside jQuery that calls the callback -- which is meaningless because that code does not expect any return value from the callback at all. - hmakholm dejó a Monica

Ahh, I see! Based on the (clear) explanation I further changed the function code to var t = $.get("time.php?a=" + Math.random() , function(data) { return data; }); return t;, expecting the change would make the alert to work now, but still something is wrong, because now it shows [object Object] (en Firefox). - Majid Fouladpour

That won't help, because you're still expecting things to happen without waiting for the callback to be executing. I don't even know what $.get returns, but it certainly isn't related to the server's reply, because $.get returns before the server has replied. Usted no puedes get the reply to appear dondequiera excepto en la devolución de llamada or in code that is called by the callback. - hmakholm dejó a Monica

¡Confirmado! los $get function returns a reference to the XMLHttpRequest object. I changed the alert part again, this time to setTimeout("alert(currenttime.responseText);",3000); and could get the date to be displayed by the alert. But as you correctly point, that responseText arrives Si lo envía después the assignment line is executed - but as we are storing a reference to the object, at the time the alert is fired, the request has completed and it has acquired a responseText: jsfiddle.net/majidf/P2JxJ - Majid Fouladpour

You are trying to return data to an anonymous function. you must set current time within the callback of the $.get action.

var currentTime;
$.get("time.php?a=" + Math.random() , 
      function(data) { // this will execute when the server request is complete & successful.
          currentTime = data;

Respondido 28 ago 11, 06:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.