views:

111

answers:

5

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!

A: 

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

PetersenDidIt
I think I skipped the last part of the question at first, but he actually says 1 is returned, so we can be positive it ISN'T 22.
crimson_penguin
+2  A: 

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.

Matt
That wouldn't matter. If it's not defined a variable will just be put in the global scope
Bob
Stupid me... thanks :P
Matt
A: 

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.

Nick Craver
He isn't returning cm, he's testing it and running something else if it's the same as hm. You may have a point about the hm variable though; it depends whether this is in global scope or not.
crimson_penguin
He's not trying to return anything - just run a function. Yes the function is being called when the AJAX request is complete, but he should still be able to work with `data` and `hm` at that time.
Pickle
@crimson_penguin, @pickle - I don't think the OP posted all of the code, and didn't say *where* the undefined was happening, so throwing out this possibility :)
Nick Craver
Oh, you're right - I was confused about the question.
crimson_penguin
+1  A: 

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.

crimson_penguin
A: 

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.

Pickle
I agree with your first suggestion, but I don't think your second paragraph is correct. In Javascript, the following is true: 22 == "22"
crimson_penguin