how do I assign a variable to a returned response in jQuery

I wanted to assign a returned value (just text) to a variable in jQuery. I wrote this:

 var hm=22;

   $.ajax({
       type: "GET",
       url: "ajax_check_match.php",                                                     
       dataType: "text",
       success:callback
       });

  function callback(data, status)
    {   
           // assign ajaxed value to cm variable                                                
    cm=data;

    if (hm != cm)
    {
    dosomething();
    }
     }

But it fails every time. Why is that the cm variable keeps getting undefined when it sends request. I set the php file to return 1 and it still says undefined. I opened ajax_check_match.php in browser and I see "1".

I didn't see the point of using XML or JSON since a simple number would suffice. Or do I have to use XML/JSON?

UPDATE: as for finding the undefined result... I wrote $("msgbox").append("hm is "+hm+ " and cm is + cm) after the callback function and cm always return undefined. hm is printed 22. It set intervals between three seconds so it keeps appending hm is 22 and cm is undefined.

NEW UPDATE: I am an idiot. Period. The ajax_check_msg.php was in the wrong location - different code. I checked the wrong page. But rest assured, I sure appreciate your suggestions!

Answers


If your PHP file is returning 1, since hm is set to 22, cm != hm is always going to be true.

cm is showing undefined... because, well, you're not defining it. Put

var cm = ''; // or something else

Somewhere to initialize it first.


I don't actually know jQuery, but looking here, it looks like you want "html" as your datatype. I would also recommend doing alert(cm); after you define it, to see what it is, rather than just testing whether it isn't one particular value.


Are you positive that 22 is getting returned? What does firebug show the response is? My bet is that its something other then just 22


AJAX happens asynchronously, meaning that your callback function happens when the response comes back, not in the order it appears in your code.

Your code happens in this order:

  1. var hm=22;
  2. $.ajax fires off
  3. The code that called this code finishes
  4. Later your response comes back, and that callback function fires.

Because of this, you can't return cm, it won't be set until the callback function runs. Instead, call whatever code needs to happen using cm from the callback function, so it'll run when the variable is ready.


Output data to see if it's what you expect. Better yet, don't set another superfluous variable, and just use if(hm != data).

hm will never equal cm or data because the former is a Number, and the latter 2 are Strings. Need to do some type shifting first.


Need Your Help

Compare Object and Long&Integer

java object compare

When I want to compare two variables(One type Object, one type Long), I found that cannot convert Object to Long, for example:

Php $_GET issue

php arrays printing foreach get

foreach ($_GET as $field => $label)