views:

553

answers:

3

Hi Every Body I'm Having Some Confusing Problem And I Need Help I Have Written Some Code To Initialize xmlHttpRequest To Send Request And Recieve Some Response,Here Is My Code:

function initRequest(url)
  {
    if(window.XMLHttpRequest){
       req=new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
     {
      isIE=true;
      req=new ActiveXObject("Microsoft.XMLHTTP");
     }
   }

function validateUser()
   {           
      var sPath = window.location.pathname;
      var sAddress = sPath.substring(0,sPath.lastIndexOf('/') + 1);
     var url=sAddress+"WebService2.asmx?op=HelloWorld";               
     initRequest(url);
     req.onreadystatechange=processRequest;
     req.open("GET",url,true);
     req.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
     req.send(null);      
  }

function processRequest()
  {
    if(req.readyState==4){
     if(req.status==200)
       {
        var message="invalid";
        alert(req.responseText);           
        //message=req.responseXML.getElementsByTagName("valid")[0].childNodes[0].nodeValue;
        //SetMessage(message);
       }
       else
       {
       alert(req.statusText);
       }
     }       
  }

My Problem Is:I Dont Know Where/How To Get The Proper XML Response From The Url Im Specifying,I Worked With Respose Object In HelloWorld Method Of My WebService But The Result Was Some DOM Architecture of a Page To Invoke The Method ...

Every Single Clue Would Be Appreciated

+2  A: 

There are 2 possible answers to this question..

  1. A long and detailed discussion about interpreting status codes and parsing results
  2. A suggestion that you look at using an established library to take the pain out of this for you.

I'm going to take the second approach and reccommend that you investigate jquery as this does all of the donkey work for you - and provides loads of other benefits too. You are then free to develop your application, not spend hours fodling with issues that have already been solved by others.

P.S. I acknowledge the existence of other javascript libraries, sucah as 'prototype', but have found jquery to meet all of my needs, as well as being included with the ASP.NET MVC framework, which makes it a no-brainer.

belugabob
Thank You So Much,For Your Solution,But I Prefere Jquery As The Final/Professional Solution For ThisDo You Have Any Suggestion For 1st Solution??
A: 

If you don't want DOM but rather XML response as a string, use responseText instead of responseXML.

Also, consider using prototype or jquery instead of writing that by hand.

If the response is different from that you've expected, maybe you're misusimg that server's api. I'd recommend reading documentation on it or tcpdumping the data exchange.

You didn't specify which server are you trying to connect to, so we can't help you on its api.

alamar
I'm Working With ASP.NET 2.0/3.5 On WinXP(IIS 6.0)My Emphesis Is On Getting Results From WebService On My ClientSide Code Actually,Getting The XML Response Is An Alternate Solution For Using This Data That Is Gained,I 1st Want To Have MyData On Client Side Code,Thanx
A: 

Here's a tutorial on using XMLHttpRequest:

Ajax Tutorial

Also, just so you know, in English, generally the only words that should start with a capital letter are the first word in a sentence, proper nouns such as names, and the personal pronoun "I".

Tobias Cohen
Thank you so much,pordon me for poor English writing(actually wrong keyboard usage)