1

I have an ajax request that send vars by get on the url and gets xml nodes back. The ajax works good, in fact I can see the response when I debug with firebug... the problem is when I try to assign the number of nodes to a var by jquery.

this is the function:

function CargarTValores(){
try{
    $.ajax({
        url: 'f_func.php?f=g_tv&adm=1',
        data: {},
        success:function(result){
            alert("entro");
            var algo = 0;
            algo = $(result).find('error').length;
            if(algo>=1){
                alert(toString($(result).find('error').first().text()));
                }
        },
        error:function(){
            alert("Error inesperado.");
        }
    });
}catch(e){
    alert("algo paso");
}
}

I know that the response of the ajax is this: <?xml version='1.0' encoding='utf-8'?><error>No existen Tipos de Valores en el sistema.</error>

so I know the length is at least 1 and if I put my mouse over the .length property on firebug it shows '1'.

by conclusion i think that my problem is on this line algo = $(result).find('error').length;

please tell what I'm doing wrong and thaks for your time ;)

Xuaspick
  • 65
  • 3

3 Answers3

1

Pass the datatype parameter in your ajax call dataType: 'xml'

Vijay Kukkala
  • 362
  • 5
  • 14
  • thank you, I feel like a total dumb I also deteled the toString function on the alert to get the message... thank you again... – Xuaspick Aug 09 '13 at 21:53
0

Parsing your html via http://api.jquery.com/jQuery.parseXML/

Example: JSFiddle

   $.ajax({
        url: 'f_func.php?f=g_tv&adm=1',
        data: {},
        success:function(result){
            alert("entro");
            var algo = 0,
            xmlDoc = $.parseXML(result),
            $xml = $( xmlDoc ),
            error = $xml.find("error");

            algo = $(result).length;
            if(algo >= 1){
                alert(error);
             }
        },
        error:function(){
            alert("Error inesperado.");
        }
    });
Christopher Marshall
  • 10,678
  • 10
  • 55
  • 94
0

You need to pareXML, here's an example (jsfiddle)

xml = "<?xml version='1.0' encoding='utf-8'?><error>
        No existen Tipos de Valores en el sistema.</error>";

xmlDoc = $.parseXML( xml );

window.alert($('error:eq(0)', xmlDoc).text());
Nabil Kadimi
  • 10,078
  • 2
  • 51
  • 58