Difficulty with AJAX picking nodes values

Asked

Viewed 164 times

0

I would like to separate this data with ajax, but I’m having a hard time with the nodes:

<?xml version="1.0" encoding="ISO-8859-1"?>
<android_ecs.xml>
<node><IAAUTNUM>2089777</IAAUTNUM></node>
<node><IAAUTNUM>2089777</IAAUTNUM></node>
<node><IAESTORNO>ESTORNADA</IAESTORNO></node>
<node><IAENTNOME>ETD052  ENTIDADE</IAENTNOME></node>
<node><IAASSNOME>TESTES -TEF</IAASSNOME></node>
<node><IAASSPORT>TESTES -TEF</IAASSPORT></node>
<node><IACARTFMT>6298 6901 0001 6680</IACARTFMT></node>
<node><IAPARPRIM>2,33</IAPARPRIM></node>
<node><IAPARCOUT>0,00</IAPARCOUT></node>
<node><IAQTDPAR>1</IAQTDPAR></node>
<node><IAVALOPE>2,33</IAVALOPE></node>
<node><IACODSESAT></IACODSESAT></node>
<node><IANOMMES>JULHO/14</IANOMMES></node>
<node><IADATA>14/07/2014</IADATA></node>
<node><IAHORA>18:39:57</IAHORA></node>
</android_ecs.xml>

    <script>
    function dados(){
    var ajax = false;
    var xml = '';
    if(window.XMLHttpRequest){
    ajax = new XMLHttpRequest;
    }else if(window.ActiveXObject){

    ajax = new ActiveXObject("Microsoft.XMLHTTP");

    }
    //ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    if(ajax){

    ajax.open("GET","http://remote.tecbiz.com.br:8080/tecbiz/tecbiz.php?     a=725d5b&ecs=5054&sen=123456&aut=&acs=3",true);
    ajax.onreadystatechange = function(){
    if(ajax.readyState == 4){
    if(ajax.status == 200){

    xml = ajax.responseText;
    android = xml.getElementsByTagName( "android_ecs.xml" )[0];
    NUMAUT = android.childNodes[0].childNodes[1].nodeValue;
    alert(NUMAUT);

    }else{

    alert('Erro!');

    }

    }

    }
    }
    ajax.send(null);
    }


</script>

Note: If I give one alert in the responseText it returns me the normal xml...the error that shows in the console is: Uncaught Typeerror: Undefined is not a Function

  • And if you use responseXML instead of responseText, in the documentation says that to return an XML object, you have to use responseXML. At a glance here in the documentation

  • The problem is that when I use responseXML it returns me null...

  • Which browser you are using?

  • I’m using CHROME, I want to get what’s between the tags!

1 answer

1

    function Xhr(){
    try {
        return new XMLHttpRequest();
    }catch(e){}
    try {
        return new ActiveXObject("Msxml3.XMLHTTP");
    }catch(e){}
    try {
        return new ActiveXObject("Msxml2.XMLHTTP.6.0");
    }catch(e){}
    try {
        return new ActiveXObject("Msxml2.XMLHTTP.3.0");
    }catch(e){}
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){}
    try {
        return new ActiveXObject("Microsoft.XMLHTTP");
    }catch(e){}
    return null;
}

function dados(){
    var ajax  = Xhr();  
    if(ajax != null){
        ajax.onreadystatechange = function() {
            if (ajax.readyState == 4 ) {
               if(ajax.status == 200){
                   /* android = xml.getElementsByTagName( "android_ecs.xml" )[0];
                    NUMAUT = android.childNodes[0].childNodes[1].nodeValue;
                    alert(NUMAUT);*/
                   var xml = ajax.responseXML;
                   var android  = xml.documentElement;
                   alert(android);                 }
               else if(ajax.status == 400) {
                  alert('400')
               }
               else {
                   alert("Erro: "+ajax.status);
               }
            }
        }
        ajax.open("GET", "http://remote.tecbiz.com.br:8080/tecbiz/tecbiz.php?a=725d5b&ecs=5054&sen=123456&aut=&acs=3", true);
        ajax.send();
    }   
}
  • Your code returns status 0, see here.

  • Of course it will go 0! you are in the jsfiddle.net domain and do an ajax on remote.tecbiz.com.br:8080 are different domains. This hurts the browser’s security policy. For it to work you must make the page that executes ajax be tbm inside the remote.tecbiz domain with.br:8080.

  • 2

    Bruno, it would be interesting to add this comment in the reply, so that it is useful not only to @Fernando but also to future users who find this answer.

Browser other questions tagged

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