AJAX javascript, PHP post does not work

Asked

Viewed 299 times

0

Good morning, I’m created a code Java to send data using AJAX to a PHP page,my problem is that the POST is going empty to page I’m calling. This only happens when I call the Javascript (AJAX) function that makes the request, when I call the PHP page straight through 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);

}
  • Puts the headers and the url. And what would be archive ?

  • the filing cabinet would be mine url, I did so because I intend to use this same function for more cases. I tried to use the header, but it hadn’t worked so I removed.

2 answers

2

Is missing the url, and define the 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.

  • the url would be my variable filing cabinet, in another attempt I tried to put the setRequestHeader, but not rolled either, the Return according to the comment below I can use a callback, however how I recover the value returned in another script js?

1

  1. Trade the Return for a callback (read this What is the real advantage of using a Callback and what is thread/multithread?)

  2. And finally adjust if to detect HTTP or connection errors:

  3. 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);
    
  • fail -> erro

  • @Edilson fail is within the scope of AjaxExecute and erro is a function outside, including tested and worked normally (the names within the scope are in English, outside would be functions created by it).

  • I’m having some doubts, the code you sent worked, but how do I get this return in another JS script? and the page is still updating, how do I make it not update?

  • I don’t get it @Rayanpereiradelima

  • I’ve been like this ajax.open("post", file, true); and so ajax.open("post", file, false);

  • I still don’t understand your doubt: "but how do I catch this return in another JS script?" @Rayanpereiradelima

  • Actually my first concern is that the page is updating yet. I searched here, but I couldn’t find anything that would help me. On the other question would be as follows, if I put that successful Function(answer) { answer Return; } would work? because I tried and can’t get that feedback in another script where I treat it. As I said I would like to use this function as generic.

  • You mean the page is changing the url in the address bar? @Rayanpereiradelima

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.