Ajax Call Tracking failed attempt

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nghivo
    New Member
    • Apr 2007
    • 17

    Ajax Call Tracking failed attempt

    I attempted to synchronize async Ajax calls using the following JS blocks:
    =============== =============== =============== =======
    [CODE=javascript]function getXMLHTTPReque st()
    {
    try
    {
    req = window.XMLHttpR equest ? new XMLHttpRequest( ): new ActiveXObject(" Microsoft.XMLHT TP");
    }
    catch (err) { }
    return req;
    }

    // update the window status
    function updateStatus()
    {
    window.status += '-';
    }

    var httpReq = null;
    function loadURL(dest, trigfunc)
    {
    // create only when needed
    if (httpReq == null) { alert("Create httpReq"); httpReq = getXMLHTTPReque st(); }

    while (1)
    {
    // only sumbit another Ajax calls when the readsyState is 0
    if (httpReq.readyS tate == 0)
    {
    httpReq.onready statechange = trigfunc;
    httpReq.open("G ET", dest, true);
    httpReq.send(nu ll);
    break;
    }
    // delay the while loop execution for 500 milisec by updating the status line
    else window.setTimeo ut("updateStatu s()", 500);
    }
    }

    // Ajax triggered function processes different business logic depending on the
    // content of responseText
    function trigFunc()
    {
    if (httpReq.readyS tate == 4)
    {
    // Process the responseText from httpReq
    ............... ...

    // to reset the httpReq.readySt ate to 0 so next Ajax calls can be submitted
    httpReq.abort() ;
    }
    }

    function myFunc()
    {
    // Test for some condition on the web page
    if (condition is true)
    loadURL(urlOne, trigFunc);

    loadURL(urlTwo, trigFunc);
    }
    [/CODE]=============== =============== ===============

    I thought after the loadURL(urlOne, trigFunc) call made, loadURL(urlTwo, trigFunc) call will be made but never submitted until the triggered function from the first call executed and reset the readyState to 0.

    While the first call is executed on the server side, window will stay in the loop and update the status.

    IT NEVER WORKED. For some reason, the result of second call is lost and window stuck in the forever loop.

    Can anyone offer some hint to my problem ? Thanks
    Last edited by gits; Jul 25 '07, 08:24 PM. Reason: added CODE tags
  • epots9
    Recognized Expert Top Contributor
    • May 2007
    • 1352

    #2
    since u are trying to run 2 requests at the same time, it won't work. cuz while one is going your overwritting it with another, so cancelling the first one.

    your httpReq object holds everything to make the ajax work, so while its running your changing its properties.

    instead, create 2 httpReq objects that way they will both run correctly.

    Comment

    • nghivo
      New Member
      • Apr 2007
      • 17

      #3
      But I tried to synch the Ajax request with the while loop
      while (1)
      {
      if (httpReq.readys tate == 0)
      { ... }
      else { .... setTimeout}
      }

      what is wrong with this code ? Thanks

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        The readystate will never return to zero if an open call has been made.
        When the state=4, it is done, grab the content and then make another call to .open() and send() and the like.

        Comment

        Working...