0

我遇到了一些令人困惑的问题,我需要帮助我已经编写了一些代码来初始化 xmlHttpRequest 以发送请求并接收一些响应,这是我的代码:

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

我的问题是:我不知道在哪里/如何从我指定的 Url 获取正确的 XML 响应,我在我的 WebService 的 HelloWorld 方法中使用了 Respose 对象,但结果是页面的一些 DOM 架构来调用该方法......

每一条线索都会被欣赏

4

3 回答 3

2

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.

于 2009-05-13T07:58:53.780 回答
1

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.

于 2009-05-13T07:59:17.260 回答
0

这是一个使用 XMLHttpRequest 的教程:

Ajax 教程

另外,您知道,在英语中,通常唯一应该以大写字母开头的单词是句子中的第一个单词、专有名词(例如名称)和人称代词“I”。

于 2009-05-13T08:35:18.450 回答