0

Why when I use the code below I never see 0 readyState even when the connection starts or is aborted?

You can check it here http://www.quemfazsite.com.br/teste1.php or using the code below

<input type="file" id="teste" />
<div onclick="aqui.abort();">abort</div>

<script type="text/javascript">

document.getElementById("teste").onchange = function() {

    aqui = new XMLHttpRequest();

    aqui.onreadystatechange = function() {

        console.log(aqui.readyState);

    };

    aqui.open("POST","http://www.example.com",true);

    var conteudo_arquivo = new FormData();
    conteudo_arquivo.append("arquivo",document.getElementById("teste").files[0]);   

    aqui.send(conteudo_arquivo);

}

</script>
amandanovaes
  • 694
  • 2
  • 7
  • 20
  • 1
    `onreadystatechange` isn't triggered by `abort()`. ([See "Note" under step 3](http://www.w3.org/TR/XMLHttpRequest/#dom-xmlhttprequest-abort)). You'll have to use [`onabort`](http://www.w3.org/TR/XMLHttpRequest/#handler-xhr-onabort) to listen for it. – Jonathan Lonowski Mar 21 '14 at 17:31
  • onreadystatechange is triggered on abort! But the readyState is 4! It should be 0! – amandanovaes Mar 21 '14 at 17:33
  • Also, [without being `var` declared](http://stackoverflow.com/questions/1470488/what-is-the-function-of-the-var-keyword-in-ecmascript-262-3rd-edition-javascript), `aqui` is a global and is changed to a different `XMLHttpRequest` with each `change`. – Jonathan Lonowski Mar 21 '14 at 17:34
  • Ok, but in my test I change select the file once so it should not be a problem the lack of var. Do you know why the specs says it should return 0 to readyState when is aborted but in my case is returning 4? – amandanovaes Mar 21 '14 at 17:35
  • Are you certain it's still `4` *after* the `.abort()`? Try modifying the `click` event to log as well -- `onclick="aqui.abort(); console.log(aqui.readyState);"`. – Jonathan Lonowski Mar 21 '14 at 17:38
  • it's 0 when i do onclick="aqui.abort(); console.log(aqui.readyState);" BUT right after that onreadystatechange is fired with aqui.readyState having the value 4 – amandanovaes Mar 21 '14 at 17:41

0 Answers0