www.webdeveloper.com
Results 1 to 7 of 7

Thread: Infamous c00c023f error

  1. #1
    Join Date
    Sep 2008
    Posts
    93

    Infamous c00c023f error

    Hello,

    The following is included in an external js file. The function "doThis()" works perfectly on one page where it is included but throws the 'Could not complete the operation due to error c00c023f' on the line for "if(xmlhttp.readyState==4 && xmlhttp.status==200)" on another page where the same js file is included.

    Search engine results seem to reference the same blog and state that this error is because the ajax call is being aborted before the readyState and status are checked. I'm not aborting it (not intentionally, anyways).

    Any ideas what might be causing this or how to fix it?

    Code:
    function loadXMLDoc(url,f){
      if (window.XMLHttpRequest){
         if(typeof xmlhttp!='undefined'){
              xmlhttp.aborted=true;xmlhttp.abort();
         }
         xmlhttp=new XMLHttpRequest();
      }else{
         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=f;
      xmlhttp.open("POST",url,true);
    }
    
    function doThis(){
       process_start();//runs animation
       loadXMLDoc("../path/myfile.php",function(){
           if (xmlhttp.readyState==4 && xmlhttp.status==200){
              //do something
           }
       });
       xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");xmlhttp.send('t='+t);
    }
    I've also tried adding the line "if(xmlhttp.aborted==true){return;}" just before the readyState line but it doesn't fix it.

    Any ideas are appreciated. Thanks!

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    776
    Because the variable xmlhttp is not being declared anywhere, using var, it becomes global and will be shared between each doThis() execution. And because of this, simultaneous requests will just overwrite(abort) the previous one.

    I would suggest either merging the two functions together and properly encapsulating the variables, or have the loadXMLDoc function create a new connection variable and pass it back. This way you can avoid conflict with the request objects.
    Last edited by bionoid; 01-25-2012 at 02:28 AM.
    JavaScript: Learn | Validate | Compact

  3. #3
    Join Date
    Sep 2008
    Posts
    93
    Thank you bionoid for your response.

    Don't I needed xmlhttp to be global so that the loadXMLDoc function will be using the same variable? In this particular example, i'm only running function doThis() once so wouldn't it only be creating and using 1 xmlhttp variable? How would it be aborting?

    By 'encapsulating', are you suggesting to put the two functions into another function together? I use loadXMLDoc in a number of scripts throughout a variety of pages for all of my AJAX calls. so I hope there is another option. Unless you think that it's worth the performance hit to have the loadXMLDoc code in every function that uses these requests?

    A possible solution might be to check if the Ajax call was aborted before checking its readyState and status but I can't seem to find a way to get it to work using the flags.

    What do you think?

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    776


    You have just revealed your problem, the xmlhttp would be shared amongst all the functions calling loadXMLDoc, so there is more than likely a confict on who own the request object at the time.

    This is how I would change your code at the moment:

    Code:
    function loadXMLDoc(url, f, d)
    {
    	var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
    	xmlhttp.onreadystatechange = function() {if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {f(xmlhttp.responseText, xmlhttp);}};
    	xmlhttp.open('POST', url, true);
    	xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    	xmlhttp.send(d || '');
    	return xmlhttp;
    }
    
    function doThis()
    {
    	process_start();//runs animation
    	loadXMLDoc('../path/myfile.php', function(response)
    	{
    		alert(response); //Received from the server
    	},
    	't=' + t);
    }
    You will see that loadXMLDoc now handles the entire request, then calls your callback function at the end.
    Because the xmlhttp is localised it should prevent requests being overwritten.
    Last edited by bionoid; 01-26-2012 at 07:04 AM.
    JavaScript: Learn | Validate | Compact

  5. #5
    Join Date
    Sep 2008
    Posts
    93
    Ah! Brilliant!

    What's the purpose of the second var "xmlhttp" in the onreadystatechange function if the callback only looks for "response"?

    Also i find 'closures' quite confusing and can never seem to keep (what I think are) local variables from being changed by running the function repeatedly. Do I have to worry about that here?

    For example, if I access loadXMLDoc from 2 different functions, how can I be sure the response will "call back" the correct function and reference the correct xmlhttp response?

    Thank you for your help.
    Last edited by speghettiCode; 01-26-2012 at 09:15 AM.

  6. #6
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    776
    I have passed back the responseText and the actual request object if you needed to retrieve more information from it. If you never do then you can remove it.

    You don't need to worry about how loadXMLDoc executes your callback, it will always be the function you passed to it at the time.
    Because f is a parameter of the function, it is basically the same as declaring it locally with var as well. Likewise for all the other declared variables.

    The problem with how you had it previously was that you were refering to the xmlhttp object in the callback, which would not have been accessible if it was declared properly.
    Last edited by bionoid; 01-26-2012 at 09:24 AM.
    JavaScript: Learn | Validate | Compact

  7. #7
    Join Date
    Sep 2008
    Posts
    93
    awesome! Thanks very much for your help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles