1


I'm a problem with AJAX, I have this error:

failed to execute 'setrequestheader' on 'xmlhttprequest' the object's state must be opened

And I have this code:

<script type="text/javascript" language="javascript">// <![CDATA[

    var myRequest = null;

    function CreateXmlHttpReq(handler) {
      var xmlhttp = null;
      try {
            xmlhttp = new XMLHttpRequest();
      } catch(e) {
            try {
                    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
      }
      xmlhttp.onreadystatechange = handler;
      return xmlhttp;
    }
    // ]]>
</script>
<script type="text/javascript">// <![CDATA[

    function processa(messaggio,codiceChat){
            myRequest = CreateXmlHttpReq(stampa_chat); //apre la funzione appendi_province
            myRequest.open('POST','script/inviamessaggio.php'); //apre carica_province.php e gli indica che i valori passati devono essere considerati POST
            myRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); //indica che il contenuto di risposta sarà di tipo html-utf8
            myRequest.send("messaggio="+messaggio+"&chat="+codiceChat); //fa passare $_POST['regione']=valore al file carica_regione.php
            myRequest.setRequestHeader("connection", "close"); //chiude la connessione ajax
            alert(messaggio);
    }

    function stampa_chat(){
            if(myRequest.readyState==4 && myRequest.status==200){
                    var a=document.getElementById("chat");
                    a.innerHTML=document.getElementById("chat").innerHTML + myRequest.responseText; //stampa il contenuto html passato all'interno dell'elemento con id="provincia"
            }
    }
    // ]]>
</script>

I have searched in this site and I have found this question: Ajax error in module: the object's state must be OPENED so I have tried to add the -> true parameter into the method myrequest.open() but it doesn't go...
What have I wrong? I don't know what I have to do... I'm literally desperate!
Thank you all,
Daniele

2
  • because you set a header after you call send... Sep 8, 2014 at 17:40
  • I'v tried to switch the position as you have said, but I have the same error. It has changes only the lane!
    – ProtoTyPus
    Sep 8, 2014 at 17:44

1 Answer 1

-1

I think you not set this :xmlhttp.setRequestHeader("X-Requested-With","XMLHttpRequest");

Now I also have this problem but I has solve it ,the reason is it

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.