Hamidreza
Hamidreza

Reputation:

xmlHttpRequest Response

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

Upvotes: 0

Views: 6268

Answers (3)

belugabob
belugabob

Reputation: 4460

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 fiddling 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.

Upvotes: 2

Tobias Cohen
Tobias Cohen

Reputation: 20000

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".

Upvotes: 0

alamar
alamar

Reputation: 19313

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.

Upvotes: 1

Related Questions