Skip navigation
Currently Being Moderated

Ajax issue

Aug 1, 2012 9:41 AM

I am having some difficulty using ajax

 

the following code DOES work

 

---------------

 

var hello = new XMLHttpRequest();

 

hello.onreadystatechange = function()

{

if (hello.readyState==4 && hello.status==200)

{

document.getElementById("myDiv").innerHTML=hello.responseText;

}

}

 

function go(){

var donkey = document.getElementById('subject').value;

hello.open("POST","gethint.php?",true);

hello.setRequestHeader("Content-type","application/x-www-form-urlencod ed");

hello.send("fname="+donkey);

}

 

------------------------

 

As you can see i start by creating my XMLHttpRequest object
bellow that is a function which is triggered by the objects readystatechange event andif the readystate is 4 it retrieves the responce
and bellow that is a function which sends data to the object, thus causing its readystate to change.

 

my question is as follows

 

the function go() is triggerd by an event elsewhere in the code (such as a click)

 

i want the readystate change function to work like this to, so i want to be able to write

 

------------------------------

hello.onreadystatechange = fire()

 

function fire()

{

if (hello.readyState==4 && hello.status==200)

{

document.getElementById("myDiv").innerHTML=hello.responseText;

}

}

--------------------------------------------
but this does not work

 

i cant understand why it does not work
it should call the fire() function when the ready state change is detected
and the fire function is written properly (i tested it by putting a alert into it and calling it with a onclick event on some div)

 

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points