473,473 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Javascript xmlhttprequest asynchoronous call requires alert

1 New Member
In the code below I have to have the alert statement marked by the two *s. Else firefox returns an empty 'temp' even though the 'webtext' is read correctly. How can I remove this requirement?

Expand|Select|Wrap|Line Numbers
  1. function getdatalinks(diagnosis,min_age,max_age,gender)
  2.     {    
  3.         var temp="";
  4.         var urii="given URL";
  5.  
  6.         if (window.XMLHttpRequest) 
  7.         {// code for IE7+, Firefox, Chrome, Opera, Safari
  8.         xmlhttp=new XMLHttpRequest();
  9.         } 
  10.         else 
  11.         {// code for IE6, IE5
  12.             xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  13.         }
  14.  
  15.           xmlhttp.open("GET",urii,true);
  16.         xmlhttp.send(null);
  17.  
  18.           xmlhttp.onreadystatechange=function()
  19.           {//the request is in ready state but not complete
  20.               if(xmlhttp.readyState==4 && xmlhttp.status==200)
  21.               {//request is complete        
  22.                   webtext=String(xmlhttp.responseText);
  23.                 alert(webtext);
  24.                 temp=webtext;
  25.               }
  26.           };
  27.             alert(temp); //**
  28.             return(temp);
  29.  
  30.     }
Feb 23 '12 #1
1 1471
gits
5,390 Recognized Expert Moderator Expert
that is the purpose of a async call. you simply cannot return the value at that point where you do it ... the response is only reliably available in the onreadystatechange-callback. so the flow of your code has to consider that and just can proceed from there. call the next function that you need from inside the callback and you are fine.
Feb 24 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Erik Veldkamp | last post by:
Hi there, When I use javascript I can call the alert(string) method, to show an alert window. How can I do the same when I use C# script? Thanks for your help
1
by: mauroalberti | last post by:
Hello everyone, I'm currently trying to add some AJAX functionality to the tree2 JSF-component from the Apache MyFaces project (i. e. if the user clicks on some node of the tree component, then...
6
by: pronerd | last post by:
Hi, I am trying to dynamically set an event handler across frames. I have no problems setting properties across frames doing something like parent.ToolMenuFrame.location.href =...
2
by: mfsiddiq | last post by:
HI I am facing some problem with my as-js communication.I am passing a String expression to a javascript function using ExternalInterface.call function upon mouse click.My string expression is as...
2
by: Fabio Mastria | last post by:
My application has to call a webpage with some hidden field, fill their values with some parameters and then call the submit to the form which has the action property set to a page of another web...
2
by: gaya3 | last post by:
Hi , can any one please say me how to retreive the array from another jsp thro' ajax call... <%@ include file="JspBean.jsp" %> <html> <head> <link rel="stylesheet" type="text/css"...
25
by: q-rious | last post by:
Hello All, I have a question regarding XMLHttpRequest. I have the following code (as part of a much larger file): function loadXMLDoc(url) { // branch for native XMLHttpRequest object if...
6
by: codeseeker1 | last post by:
currently iam working with a php code along with ajax. code works properly with an alert box.when alert box is removed,rest of code not working.can anyone help? thanx in advance
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.