AJAX javascript, PHP post does not work [closed]

0

Hello, I have a javaScript code to send data using AJAX to a PHP page, my problem is that the POST is going empty for the page I am calling. This only happens when I call the JavaScript (AJAX) function that does the request, when I call the PHP page direct by the action POST works normally. What can it be?

 function AjaxExecute(arquivo){
        var ajax;

        if(window.XMLHttpRequest){ 

            ajax = new XMLHttpRequest();

        } else {

            try{
                ajax = new ActiveXObject("Msxml2.XMLHTTP");
            }catch(e){
                ajax = new ActiveXObject("Microsoft.XMLHTTP");
            }   
        }

        ajax.onreadystatechange = function(){

            if(ajax.readyState == 4 && ajax.status == 200){

                return this.responseText;
            }
        }

        ajax.open("post",arquivo);
        ajax.send(null);

}
    
asked by anonymous 15.04.2017 / 15:57

2 answers

2

You are missing url , and set header :

...
var url = "receberFicheiro.php";
ajax.open("post", url, "true");
ajax.setRequestHeader("Content-type", "multipart/form-data");
ajax.onreadystatechange = function(){
   if(ajax.readyState == 4 && ajax.status == 200){
      # ???
      return this.responseText;
   }
}
ajax.send(ficheiro);

And attention to return .

    
15.04.2017 / 19:38
1
  • Change the return to a callback (read this What is the real advantage of using a CallBack and what is thread / multithread? )

  • And finally set the if to detect HTTP or connection errors:

  • Then adjust the header to use POST (read this AJAX Javascript Pure Asynchronous )

    function AjaxExecute(arquivo, variaveis, success, fail){
        var ajax;
    
        variaveis = !variaveis ? null : variaveis;
    
        if(window.XMLHttpRequest){ 
    
            ajax = new XMLHttpRequest();
    
        } else if (window.ActiveXObject) {
    
            try{
                ajax = new ActiveXObject("Msxml2.XMLHTTP");
            }catch(e){
                ajax = new ActiveXObject("Microsoft.XMLHTTP");
            }
        } else {
            //Simula um erro acaso Ajax não esteja disponivel (quase improvavel)
            setTimeout(error, 1, 0);
            return;
        }
    
        ajax.onreadystatechange = function(){
    
            if(ajax.readyState == 4){
                if (ajax.status == 200) {
                    //Callback se retornar status HTTP 200
                    success(this.responseText);
                } else {
                    //Calllback se retornar qualquer outro status até 0 (sem conexão)
                    fail(ajax.status);
                }
            }
        }
    
        ajax.open("post", arquivo);
    
        //Ajusta a requisição para trabalhar o POST
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    
        //Envia os dados via POST
        ajax.send(variaveis);
    }
    
    function sucesso(resposta) {
        alert(resposta);
    }
    
    function erro(codigo) {
        alert("Erro na requisição:" + codigo);
    }
    
    AjaxExecute("pagina.php", "foo=1&bar=2", sucesso, erro);
    
  • 15.04.2017 / 20:48