Interpreting XML with JQuery

3

I made a script with JQuery to use AJAX and it's working. The problem is when interpreting the XML is returned, the XML is the result of a login attempt, it contains information indicating whether the user was authenticated or not, otherwise it contains an error description:

<result id='0 ou 1'>erro caso exista ou fica vazio</result>

But at the time of reading this:

success: function(data){
                var s = $(data).find("result").each(function(){

                    var id = $(this).attr('id');

                    if(id == 0){
                        var error = $(this).text();
                        $(".error_description").text(error);
                        $(".error_description").css(display:"block");
                    }
                    else{
                        $(window.document.location).attr('href', "/Snotes/user_panel.php?id=" + id);
                    }
                });
            }
                    
asked by anonymous 29.04.2014 / 16:15

1 answer

3

You can use $.parseXML()

Html

<div id="id">id: </div>
<div id="texto">Texto: </div>

jQuery

var xml = "<result id='0 ou 1'>erro caso exista ou fica vazio</result>";
xmlDoc = $.parseXML( xml );
$xml = $(xmlDoc).contents();
$("#id").append($xml.attr("id")); // adiciona "0 ou 1" à div
$("#texto").append($xml.html()); // adiciona "erro caso exista ou fica vazio" à div

As you can see working in this jsfiddle .

    
29.04.2014 / 18:19