Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6232177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:04:20+00:00 2026-05-24T10:04:20+00:00

I’m calling 2 JS functions(Ajax) each of which are returning data from the server.But

  • 0

I’m calling 2 JS functions(Ajax) each of which are returning data from the server.But the 2nd function gets the responsetext of the 1st function.

one thing i observed is,if i use 1 or more alerts in the functions(to know what the data is) before using it(printing an image and displaying an anchor) it works fine as in — given a bit of delay(i’m guessing) its working fine,without which it fails.

i find this very strange and once i did some searching found this article( http://forums.devnetwork.net/viewtopic.php?f=13&t=117523 ) in which he has a workaround — give a timeout of 1 or .5 sec’s and things will be proper.

Although the workaround seems to get the job done i’m curious to know why the response text is getting values from the previous Ajax.

I know the code is not necessary.but,just in case — its given below

GIST:

1.the alerts when included — the code works fine

2.When the alerts are removed the Anchors are getting displayed but not the image is a Crushed image(Sort of a torn paper kinda image) on checking the image info did i find the data form the previous function — hence Not getting displayed.

<style type="text/css">
.image{
position: relative;
left: 20%;
}
</style>

<script type="text/javascript" >
function create(){
alert("createUid"); // 1st alert with which the code works
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
    var json = eval(xmlhttp.responseText);
    for(i=0;i<json.length;i++){     
        var a = document.createElement("a"); //creates a List of anchors
        a.id="a"+json[i].uid;
        a.style.display="block";
        a.setAttribute("href","#");
        a.setAttribute("onclick","alert("+json[i].uid+")"); 
        a.appendChild(document.createTextNode(json[i].uid));
        document.getElementById("tag").appendChild(a);
        }
    }
}
xmlhttp.open("GET","users.php?send=vivek",true);
xmlhttp.send("NULL");

}
function display(){
alert("pict");
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
        var img = document.createElement("img"); 
        img.id="dispPict";
        img.class="image";
        img.setAttribute("alt","ViperRules!!");             
        img.setAttribute("src","data:image/jpg;base64,"+xmlhttp.responseText); // response text is a binary of an image stored in the DB.
        img.style.display="block";
        document.body.appendChild(img);
        //document.getElementById("Tag").appendChild(img);
        }
    }   
xmlhttp.open("GET","users.php?pict=vivek",true);
xmlhttp.send("NULL");
}
 </script>

<body onload=" display(), create();">
<div id="tag" class="image"> </div>
</body>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:04:21+00:00Added an answer on May 24, 2026 at 10:04 am

    You are using the global variable xmlhttp in each function–meaning each function uses the same xmlhttp. The second function called replaces the first function’s xmlhttp onreadystatechange so when the first call returns, it executes the second’s onreadystatechange.

    To fix this, you need to not use a global variable. This can be done by addding var in front of the variable when it’s defined.

    Example:

    <script type="text/javascript" >
    function create(){
        alert("createUid"); // 1st alert with which the code works
        var xmlhttp=new XMLHttpRequest();
        ...
    }
    
    function display(){
        alert("pict");
        var xmlhttp=new XMLHttpRequest();
        ...
    }
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.