0

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); 
            });
        }
    }
});
7
  • As you've only provided a fragment it's hard to say for sure, but I don't think your XML is valid. There doesn't seem to be a root element (which might jut be because of the fact it's a fragment and not the whole thing), and I don't think you're allowed to have mixed case tags or dot separated tags. If the XML isn't valid trying to manipulate it with javascript can be problematic. Can you run it through the w3 validator and see what it says? Commented Jun 21, 2011 at 9:22
  • @GordonM: I got it by using alert($(xml).find('sessiontoken').text()); Commented Jun 21, 2011 at 9:23
  • why are you using raw XMLHttpRequest code when you've got JQuery? Most of that code could be replaced with a single call to $.ajax() or one of the other similar JQuery functions. Commented Jun 21, 2011 at 9:25
  • @Spudley: can you give an example in this context? Commented Jun 21, 2011 at 9:32
  • @Abhishek - as I say, JQuery provides AJAX functionality, which wraps all that XMLHttpRequest functionality into a single easy to use function. It's a bit out of scope for this question, and a good example would be hard to fit into a comment, so I'd suggest reading the JQuery manual pages for the $.ajax()](http://api.jquery.com/jQuery.ajax/) and [$.post() functions, etc. Commented Jun 21, 2011 at 9:37

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks i made the 1st xml separate from the other 1. now it works. Thanks
0

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);                        
          });                     

    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.