ajax xmlhttp.responsetext problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
dave1909
Forum Newbie
Posts: 22
Joined: Sat Jul 25, 2009 8:56 pm

ajax xmlhttp.responsetext problem

Post by dave1909 »

On my website, i have a button which when clicked should fill 3 <div>s. The Divs all have separate IDs and are in different places on the page so i cant combine them into one. My problem is that the xmlhttp.responseText appears to be getting mixed up and writing the wrong responsetext in each <div>. There doesnt appear to be any particular pattern to this. It happens about 80% of the time.

Here's the code for one of the functions. The other 2 are identical except for the link and the div they update.

Code: Select all

function updatelist(round)
{
if (window.XMLHttpRequest)  {  xmlhttp=new XMLHttpRequest();  }
else  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {

    document.getElementById("mailboxlinks").innerHTML=xmlhttp.responseText;
    }
  }
  var thelink = "mailboxlinks.php?x=" + round;
xmlhttp.open("GET",thelink,true);
xmlhttp.send();
}
I have a feeling that the xmlhttp.responseText is getting mixed up. How can i clarify it to stop this happening?

I've changed the link variables so they're all different and it doesnt do it as often but if any of the responsetext is blank, they all go blank
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Re: ajax xmlhttp.responsetext problem

Post by Phoenixheart »

You should try a Javascript library to avoid all these headaches. jQuery is highly recommended.
dave1909
Forum Newbie
Posts: 22
Joined: Sat Jul 25, 2009 8:56 pm

Re: ajax xmlhttp.responsetext problem

Post by dave1909 »

I've been looking through the jquery website for ages now and i've concluded that it would take so much longer to learn that and put it in that its not worth bothering with.

In this situation, it just looks like it'll make it 50 times more complicated and just give me even more headaches
dave1909
Forum Newbie
Posts: 22
Joined: Sat Jul 25, 2009 8:56 pm

Re: ajax xmlhttp.responsetext problem

Post by dave1909 »

I've managed to solve the problem by just calling one function when the div is clicked, calling the 2nd and 3rd functions inside the first and using setTimeout at 1 and 2 seconds. Seems to be working fine
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Re: ajax xmlhttp.responsetext problem

Post by Phoenixheart »

dave1909 wrote:I've been looking through the jquery website for ages now and i've concluded that it would take so much longer to learn that and put it in that its not worth bothering with.

In this situation, it just looks like it'll make it 50 times more complicated and just give me even more headaches
I would disagree. The learning curve for jQuery is to me way shorter than "traditional" self-written AJAX. Anyway, just a matter of opinion.
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: ajax xmlhttp.responsetext problem

Post by PHPHorizons »

If a person only uses jQuery once, the learning curve is going to be high. But if a person takes an hour or two to learn how jQuery ajax works and then uses jQuery for many years afterwords, that person will have saved a tremendous amount of time in the long run.

A javascript library makes many common tasks in javascript far easier and greatly increases productivity. *This is not an opinion, but is an irrefutable statement of fact.*

Cheers

PS I do applaud folks that want to learn the underlying javascript, but folks that disregard javascript libraries wholesale make a big mistake.
Post Reply