Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

ASP.NET Forum


You are currently viewing our ASP.NET Forum as a guest. Please register to participate.
Login



Reply
Getiing values with Ajax but ... Please take note of the alert results.
Old 06-09-2008, 04:25 PM Getiing values with Ajax but ... Please take note of the alert results.
Sleeping Troll's Avatar
Ultra Talker

Posts: 351
Name: Butch Begy
Trades: 0
Code:
 
<script>
 
var OrderData
 
<!--
 
function GetOrder(){
try
 {
 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
 }
catch (e)
  {
  try
   {      
   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
   }
    catch (e)
      {      
    alert("Your browser is not supported");
    return false;
    }    
  }  
   xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
    {
     OrderData=xmlHttp.responseText;
     alert(OrderData)//This alert returns values
  }
  }
 var ReqString = "CustID="+CustID;
  xmlHttp.open("GET","GetItems.asp?"+ReqString,true);
 xmlHttp.send(null);
}
 
 
function Init()
{
OrderData=GetOrder()
}
alert(OrderData)//This alert returns "undefined"
-->
</script>
HTML Code:
</head>
<body onLoad="Init();">
Sleeping Troll is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-09-2008, 06:48 PM Re: Getiing values with Ajax but ... Please take note of the alert results.
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
function Init()
{
OrderData=GetOrder()
}
alert(OrderData)//This alert returns "undefined"
Well it would, seeing as how you set the value of OrderData to be a function NOT the value from the function. Because the function does NOT return a value.

you are missing the return xmlHttp.responseText; from the AJAX call

Your code is a bit of a mish mash in rather poor VB style code by trying to use a global scoped variable to pass values instead of allowing the function to actually return a value.

javascript & JScript are rather less forgiving on sloppy code than VB/VBScript is.

to use the code syntax you had, the Init() call should be
Code:
function Init()
{
GetOrder()
alert(OrderData)
}
Using a globally scoped variable for passing values between sub / function calls is poor practice and will lead to more and more problems as your application grows.
If you need a global value, define as a read only property, it is much safer if your code cannot accidentally redefine it or overwrite the value.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 06-09-2008, 07:01 PM Re: Getiing values with Ajax but ... Please take note of the alert results.
Sleeping Troll's Avatar
Ultra Talker

Posts: 351
Name: Butch Begy
Trades: 0
All good advice Chris, however I originally used the return and the syntax:
function Init()
{
GetOrder()
alert(OrderData)
}

same result. Your advice on Global variables is good, but(notice I referred to my thesaurus to avoid using however again!) There are 10 variables that are common to most pages in my site and I am passing them via querystring. In the case of OrderData, by the by(nevermind) I am using a global variable out of pure desperation! As a global variable I cannot understand how one alert sees it and the other doesn't? Any ideas?

P.S. Really impressed by your ability to interpret code!

Last edited by Sleeping Troll; 06-09-2008 at 07:12 PM..
Sleeping Troll is offline
Reply With Quote
View Public Profile
 
Old 06-09-2008, 07:21 PM Re: Getiing values with Ajax but ... Please take note of the alert results.
nyef's Avatar
Ultra Talker

Posts: 265
Name: Lucas
Trades: 0
The ajax call is asynchronous. When you call GetOrder(), it may take a second or two for it to get the results. When you call alert(OrderData) (which should be INSIDE the init function btw) immediately after calling GetOrder, OrderData still hasn't been defined (and won't be for another second or two).

You can initiate a loop and wait for OrderData to be populated:
Code:
for (var haveData=false;!haveData;haveData=(typeof(OrderData)!='undefined')));
Or you could just move the order processing code to inside the onreadystatechange function.
__________________
~nyef

Please login or register to view this content. Registration is FREE
nyef is offline
Reply With Quote
View Public Profile Visit nyef's homepage!
 
Old 06-09-2008, 07:23 PM Re: Getiing values with Ajax but ... Please take note of the alert results.
Sleeping Troll's Avatar
Ultra Talker

Posts: 351
Name: Butch Begy
Trades: 0
By George, I think you have it! Thank you, Thank you very much!

Arrrgh! the first alert does not occur until the readystate change test = 4!

If the cause was async both alerts would return "undefined", this is really perplexing how is it possible that 2 alerts on the same page can see a global differently?

Last edited by Sleeping Troll; 06-09-2008 at 07:39 PM..
Sleeping Troll is offline
Reply With Quote
View Public Profile
 
Old 06-12-2008, 12:10 AM Re: Getiing values with Ajax but ... Please take note of the alert results.
nyef's Avatar
Ultra Talker

Posts: 265
Name: Lucas
Trades: 0
He's right, you need to change OrderData=GetOrder() to just GetOrder() (drop the "OrderData=")...

The way you have it now, when you call GetOrder(), it executes the call for the xmlhttp, then sets OrderData to be the results. But then in the Init() function, right after GetOrder() is called, the variables OrderData is set AGAIN to be the return value of the GetOrder() function. Since that return value was never specified, it is setting the OrderData variable twice, once inside the GetOrder() function to the results of the http call, then again outside the GetOrder() function but inside the Init() function, it sets it to 'undefined' the second time since there is no return call.
__________________
~nyef

Please login or register to view this content. Registration is FREE
nyef is offline
Reply With Quote
View Public Profile Visit nyef's homepage!
 
Reply     « Reply to Getiing values with Ajax but ... Please take note of the alert results.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.61302 seconds with 12 queries