Interpreting XML with Jquery

Asked

Viewed 191 times

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, if not, it contains an error description:

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

But when it’s time to read 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);
                    }
                });
            }

The code was inspired by the tutorials of several web posts, but is not working, and now that Google Chrome no longer wants to stop us breakpoint within the function each, it gets harder.

  • 3

    $(".error_description").css(display:"block"); Was it a typo writing the question, or is your code that way? (Keys were missing: css({display:"block"})) In addition, on condition of success the action That’s the way it is, or you didn’t mean $("#go")[0].action? (i.e. this attribute - which I don’t know - is from the jQuery object or the DOM element?)

  • Fixed, the ("#go") I found on the web, a redirect method, is just like that

  • Hmm, it looks like #go might actually be an element of the html page, I’ve already modified there as well.

1 answer

3


You can use the $.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 running in this jsfiddle.

  • It worked now, I appreciate the help.

Browser other questions tagged

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