1

I am using long polling in my project. When the project is running, one of the long polling requests is waiting response from the server. Now, when the user generates a new template, then I have to abort the previous long polling call and run the new one.

Aborting in IE gives the error

couldn't complete the operation due to error c00c023f 

I don't want to use the Jquery and aborting the previous request is very important. I tried Try and Catch to prevent from showing this error but it didn't work out.

I am also using the abort when the normal request(not polling request) request doesn't reach the server after few seconds, then I abort the old request and send new one.

I store the xmlHttp object in array for each request.

like: array_XMLHttp.push(request1);

I abort it like: array_XMLHttp[index_of_request].abort();

I couldn't find any solution for IE9, while it works perfect on all other browsers.

UPDATE:

I always check the status and readyState for the XMLHttp Object as below:

function checkResponse(xmlHttp){
    xmlHttp.onreadystatechange=function(){ 
        if(xmlHttp.readyState==4){
            if(xmlHttp.status==200){   
                // Request received
            } 
        }
    }
}      


Here is how I start the request

// to start new XmlHttp object
var xmlHttp=crXH();         

// add the request to array to be aborted later if required
array_XMLHttp.push(request1);

// initiate the callback
checkResponse(xmlHttp);

//send the request to server
send(xmlHttp);


When I abort:

// The problem now the callback function will not read (undefined) the new property added to the aborted object.
array_XMLHttp[index_of_request].aborted = true; 
array_XMLHttp[index_of_request].abort();

Thank you for your help!

moderns
  • 650
  • 10
  • 23
  • 1
    Possible duplicate: http://stackoverflow.com/questions/7287706/ie-9-javascript-error-c00c023f – jfriend00 Jun 24 '14 at 00:11
  • 1
    Possible work-around: http://answer.techwikihow.com/368561/script575-complete-operation-due-error-c00c023f.html – jfriend00 Jun 24 '14 at 00:13
  • I don't think it is a duplicate. My error happens when I abort the request. Thanks I will look into it. – moderns Jun 24 '14 at 00:14
  • It didn't help me because I need to abort the request. – moderns Jun 24 '14 at 00:15
  • The accepted answer to [that question](http://stackoverflow.com/questions/7287706/ie-9-javascript-error-c00c023f) discusses aborting the request and how to work around the error that occurs after aborting. I wouldn't be so hasty to conclude that answer doesn't have to do with aborting because it appears to address exactly that. – jfriend00 Jun 24 '14 at 00:16
  • And, all work-arounds I've seen say that the error goes away if you test for `readyState === 4` before you look at any other properties of the XMLHttp object so you avoid looking at those other properties when not in that `readyState`. – jfriend00 Jun 24 '14 at 00:18
  • What does the full code look like for the request? – epascarello Jun 24 '14 at 00:28
  • @jfriend00 would you please look at the update on the question? I always check the status and readyState for the XMLHttp. Appreciate your input. But I need to abort the long polling request which waits for 50 seconds before getting response from the server – moderns Jun 24 '14 at 00:28
  • 1
    There's a lot written about error c00c012f and Ajax abort in IE. A google search shows hundreds of posts. From what I can tell, the common theme is the work-around shown in this article: http://www.enkeladress.com/article/internetexplorer9jscripterror. The issue is once you abort, IE triggers onreadystatechange, but it may thrown an exception if you try to access common properties on the object like `.readyState` or `.status`. The work-around is to set your own `.aborted` property when you abort and check that first to avoid checking the other exception-throwing properties on an abort. – jfriend00 Jun 24 '14 at 00:39
  • @jfriend00; Thanks a lot! You are great. I did understand your point and I am trying to find a way to stop the call back function and restart it again after the abort process because the xmlHttp object is sent by value as you can see in the updated code and when I add a property (aborted), I can't read it on the callback function. If you can suggest something I would be so thankful. – moderns Jun 24 '14 at 02:11
  • 1
    You haven't included enough of the relevant code for me to understand what you're asking for help with. You put the `.aborted` property on the `xmlHttp` object when you call `.abort()` which you have to have access to at that time and you also have access to in `checkResponse()` which are the two places you need it. – jfriend00 Jun 24 '14 at 02:14
  • Thanks. My question is simple, after I add the property (aborted) and then abort the request, this property (aborted) wont be updated in the xmlHttp object which is executed inside the callback function, because when I initiate the xmlHttp object, I initiate the callback function by sending the xmlHttp object to the callback function which is checkResponse(xmlHttp). I just have updated the code – moderns Jun 24 '14 at 02:25
  • I meant the value of the property (aborted) in the callback will be undefined like: alert(xmlHttp.aborted) = undefined – moderns Jun 24 '14 at 02:35
  • 1
    Objects like the xmlHttp object are not passed as copies. So, the object that you see in your callback is the exact same object as exists everywhere else. It doesn't matter that it's passed as an argument to a function. That function argument still points to the original object and should show the property. Did you actually try it? – jfriend00 Jun 24 '14 at 02:37
  • @jfriend00; I really don't know how to thank you! It worked out now! I want to give this answer to you, if you want just write a short answer and I will edit it and show the full code to explain to others. I am greatly appreciate your help. – moderns Jun 24 '14 at 02:58

1 Answers1

1

Codifying our long discussion into an answer...

Per various articles like this one I've found on this topic (it's not an unusual issue in IE), IE does not like you to access the standard properties on the xmlHttp object after you call .abort(), but it will still trigger some onreadystatechange events which causes you to try to access those standard properties, thus causing an error.

The work-around is to set your own property on the xmlHttp object when you call .abort() and in your readystatechange handler, you first check your own .aborted property and if it's set, you then know the call has been aborted and you don't check the other properties which can cause the problem.

So, if your xmlHttp object is called x, then you'd do this:

x.readystatechange = function() {
    if (!x.aborted) {
        if(xmlHttp.readyState==4){
            if(xmlHttp.status==200){   
                // Request received
            } 
        }
    }
}

And, when you want to abort:

x.aborted = true;
x.abort();
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • It works like a charm! Thank you very much for helping me with this solution. Appreciate the great hints provided. – moderns Jun 24 '14 at 03:54