No offense taken Mike. Andy and I were pounding our heads trying to figure out why the value wasn't appearing so by using the synchronous call, I was able to figure out that the variable outside of the $.get() callback was indeed being filled.

You're right, though, that a synchronous call should be a last resort.

Rey

Michael Geary wrote:
Rey, no offense, bud, but synchronous ajax is a last resort. It freezes the
browser while the ajax data is loaded.  You don't want to use it unless
you're certain that it's necessary - and that is rare.

Andy, the real question is what you want to do. Scope is not a problem: You
can easily assign to a variable in an outer scope:

  var saveResult;
  $.get("ajax.cfm",function(result){
      saveResult = result;
});
But you can't access saveResult immediately after $.get() returns as in your
example. After ajax.cfm is loaded and the callback runs, then saveResult is
available to any of your other code.
If you *must* be able to write code that works exactly like your example:

  $.get("ajax.cfm",function(result){
      // callback body
  });
  alert(result);

then synchronous ajax is the only way to do it. But freezing the browser
while the ajax call runs is something to avoid.

What is the actual goal you want to accomplish? Or were you just curious why
your example didn't work as you expected?

-Mike

From: Rey Bango

Hey Andy,

I found out what's happening. You are in fact updating the var but
$.get() being an async method, will take longer to complete than the rest of the JS script. Here's a small example:

<script language=JavaScript>
$(document).ready(function() {

        foo = 'test 1';

        $.get("module.cfm", function(result){
            foo = 'Test 2';
       });
        
        alert( foo );
  });

</script>         

<a href="javascript:alert(foo);void(0);">Go</a>

You'll see that when you run this, the first alert box will come up saying "Test 1". Thats because $.get is processing the request asynchronously. When you click on the link, you'll then see that the value of foo has changed to 'Test 2'.

Your best bet is to use this code:

foo = 'test 1';

$.ajax({
url: "module.cfm",
async: false,
success: function(msg){
        foo = 'Test 2';
  }
});

alert( foo );

$.ajax() lets you define where to make the call synchronous and asynchronous.

I told you I'd figure it out!! :)

HTH.

Rey././

Andy Matthews wrote:
I'm receiving a string of text back in a .get call:

I can alert the "result" variable while within the callback
portion of
the .get call.
$.get("ajax.cfm",function(result){
        // callback body
        alert(result);
});

But I'd like to somehow be able to get that result variable outside the callback like so:

$.get("ajax.cfm",function(result){
        // callback body
});
alert(result);

But it doesn't seem to be working. Surely .get has to be able to interact with scope outside of itself. Am I missing something?



--
BrightLight Development, LLC.
954-775-1111 (o)
954-600-2726 (c)
[EMAIL PROTECTED]
http://www.iambright.com

Reply via email to