AabinGunz
AabinGunz

Reputation: 12347

XML Parsing in jquery for a XMLHttpResponse

I get the below response, i want to get value of sessiontoken ie., -4611685691785827288

<com.abc.csm.common.LoginResponse>
<sessiontoken>-4611685691785827288</sessiontoken>
<isSuccess>true</isSuccess>
<_-print_-names>false</_-print_-names>
 <_-hash_-code>0</_-hash_-code>
</com.abc.csm.common.LoginResponse><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0     Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
... and so on

So I wrote below code to get what i want, but instead it gives me undefined

var url_action="/csm/login.action";
             var client; 
             var dataString;

             if (window.XMLHttpRequest){ 
                 client=new XMLHttpRequest();
             } else {                    
                 client=new ActiveXObject("Microsoft.XMLHTTP");
             }

             client.onreadystatechange=function(){

                 if(client.readyState==4&&client.status==200)
                 {
                     xml=client.responseText;
                     $(xml).find("com.abc.csm.common.LoginResponse").each(function()
                    {
                         sessiontoken= $(this).find('sessiontoken').text();

                    });
                    alert(sessiontoken); //here i get undefined
                 }
             };

             dataString="emailaddress="+document.getElementById("email_id").value+"&projectid="+document.getElementById("project_id").value;
             client.open("POST",url_action,true);
             client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

             client.send(dataString);

Edited after @Vivek: answered

$(function() {
    $("#loginSubmitBtn").click(submitLogin);

    function submitLogin()
    {
        if(validate()) //this is true
        {
            $.post("/csm/login.action",function(xml) {
                alert(xml); 
            });
        }
    }
});

Upvotes: 0

Views: 365

Answers (2)

Vivek
Vivek

Reputation: 11028

i think in jquery you can shorten your code and can increase readability too.

$.post(url,function(xml) {
        $(xml).find("com.abc.csm.common.LoginResponse").each(function()         {                          
            sessiontoken= $(this).find('sessiontoken').text();  
            alert(sessiontoken);                        
          });                     

    });

Upvotes: 0

Spudley
Spudley

Reputation: 168803

What you're basically transmitting is two separate XML files joined together -- you've got the one which starts with the <com.abc.csm.common.LoginResponse> tag, and then another which is the XHTML document.

But you're treating them as a single XML document. The problem is that a single XML document must have only one root element; in other words, it must have a single element that wraps around the whole document. In your case, this isn't true, because <com.abc.csm.common.LoginResponse> is the root element, but then ends, and <html> is then started as a new root element.

Because of this, it can't be parsed by an XML parser, which is what you're trying to do when you call $(xml).

The solution to this is either to provide a wrapper XML element around the whole document, or to split the output into the two separate chunks before trying to parse it as XML.

Upvotes: 1

Related Questions