Ajax problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theS70RM
    New Member
    • Jul 2007
    • 107

    Ajax problem

    hi guys,

    My code suddenly stopped working

    here it is:


    [HTML] function Ajax(page,data) {

    page = "includes/" + page;

    var xmlHttp;
    try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest( );
    }
    catch (e)
    {
    // Internet Explorer
    try
    {
    xmlHttp=new ActiveXObject(" Msxml2.XMLHTTP" );
    }
    catch (e)
    {
    try
    {
    xmlHttp=new ActiveXObject(" Microsoft.XMLHT TP");
    }
    catch (e)
    {
    alert("Your browser does not support AJAX!");
    return false;
    }
    }
    }

    var output;

    xmlHttp.onready statechange=fun ction()
    {
    if(xmlHttp.read yState==4 || xmlHttp.readySt ate=="complete" )
    {
    output = xmlHttp.respons eText;
    }
    }

    xmlHttp.open("P OST",page,false );
    xmlHttp.setRequ estHeader('Cont ent-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send(da ta);


    return output;

    }[/HTML]


    It works in firefox 2 (with firebug installed and enabled) but onreadystatecha nge doesnt seem to get called in IE (7 and before) and firefox 3.

    Can anyone see why?? It has been working fine and I dont think ive changed this function but after an afternoon of debugging ive narrowed it down to this error. I also just tried replacing the "output = xmlHttp.respons eText;" line to use a callback function like most online examples do, but still no joy.


    Cheers

    Andy
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You're making a synchronous request as opposed to an asynchronous one. Are you sure that's what you want?

    Comment

    • theS70RM
      New Member
      • Jul 2007
      • 107

      #3
      is that because im making the ajax function return the output from the request rather than using a callback function?

      I think its ok that way for what im doing as its processing a form and the page is frozen till the action is complete anyway...

      Not my main problem though, and thats that onreadystate doesnt get called anymore.

      cheers for the response

      Andy

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        If it's synchronous, the onreadystatecha nge will never fire, so output won't be set. Return the responseText directly after send() in place of 'output'.

        Comment

        • theS70RM
          New Member
          • Jul 2007
          • 107

          #5
          ok, im a little lost then.

          Thanks, that works by the way!

          How would you make this function asynchronous?

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Since it's synchronous, it never needs to let you know when it's complete via onreadystatecha nge. You'll know when the browser stops hanging. It stops at send() and waits until the request is complete, so you can safely return responseText after it, a bit like how a prompt/alert is modal.

            To make it asynchronous, use "true" for the last parameter of the open() method.

            Comment

            • theS70RM
              New Member
              • Jul 2007
              • 107

              #7
              ah ok, i think i understand now.

              Thanks for your help!

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                No problem at all, you're welcome :)

                Comment

                Working...