0 Replies Latest reply: Aug 1, 2012 9:41 AM by iconofsin14 RSS

    Ajax issue

    iconofsin14 Community Member

      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-urlencoded");

      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)