简体   繁体   中英

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


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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM