Problem in the ajax

Asked

Viewed 31 times

0

I am not getting the answer in the ajax request. The code does not confirm whether it is working or not.

<script type="text/javascript">
    function iniciarAjax(){
        var ajax = false;
        if(window.XMLHttpRequest){
            ajax = new XMLHttpRequest();
        }else if(window.ActiveXObject){
            try{
                ajax = new ActiveXObject("Msxml2.XMLHttpRequest");
            }
            catch(e){
                try{
                    ajax = new ActiveXObject("Microsoft.XMLHttpRequest");
                }catch(ex){
                    ajax = false;
                }
            }
        }
        return ajax;
    }
    var conectarServidor = iniciarAjax();
    if(conectarServidor){
        conectarServidor.onreadystatechange = function(){
            if(conectarServidor.readyState == 4){
                if(conectarServidor.status == 200{
                    alert("Tudo certo")
                }
                else{
                    alert("Problema ocorreu")
                }
            }
            conectarServidor.open("GET","http://localhost/locadora/cadastro.php");
            conectarServidor.send();
        }
    }
</script>
  • I put the code you used in the answer here, to be in the question and not in an answer, because it was part of the question. You can always click [Edit] to add details to the question.

1 answer

1

The lines

conectarServidor.open("GET","http://localhost/locadora/cadastro.php");
conectarServidor.send();

have to test out of .onreadystatechange = function(){ for that is the function that will run when ajax starts to change its state. Having it in there it will never run because the .send() was never executed.

function iniciarAjax() {
  var ajax = false;
  if (window.XMLHttpRequest) {
    ajax = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      ajax = new ActiveXObject("Msxml2.XMLHttpRequest");
    } catch (e) {
      try {
        ajax = new ActiveXObject("Microsoft.XMLHttpRequest");
      } catch (ex) {
        ajax = false;
      }
    }
  }
  return ajax;
}
var conectarServidor = iniciarAjax();
if (conectarServidor) {
  conectarServidor.onreadystatechange = function() {
    if (conectarServidor.readyState == 4) {
      if (conectarServidor.status == 200) {
          alert("Tudo certo")
        } else {
          alert("Problema ocorreu")
        }
      }
    }
  }
  conectarServidor.open("GET", "http://localhost/locadora/cadastro.php");
  conectarServidor.send();
}
  • I did as you had mentioned. I put it away. But it still doesn’t work.

  • @Alexandresantosdacruz is missing one ) after 200, take a look here: http://jsfiddle.net/8zdjcyna/

  • Thank you so much for your help. So my problem was to have put the code inside the keys?

  • A question. The code now answers whether it’s working or not. When I put the url to the php page as the POST method that I created is accusing an error. So, the problem is in it?

  • 1

    I got it, I got it. Thank you very much.

  • @Alexandresantosdacruz great. If the answer solved your problem you can click and mark as accepted.

Show 1 more comment

Browser other questions tagged

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