XMLHttpRequest responseXML始终为null

时间:2013-07-10 21:44:03

标签: javascript ajax xml web-services debugging

我正在调用像这样的asmx网络服务

var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }      
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {
            var data = xmlhttp.responseText;
            var xmlDoc = xmlhttp.responseXML;
        }
    }
    xmlhttp.open("GET", "https://Service/ServiceName.asmx/method?query=data1&count=1",true);
    xmlhttp.setRequestHeader('Content-Type', 'text/xml');
    xmlhttp.send();

即使在readystate为4之后,我也将responseXML视为null,将responseText视为空。而网址

"https://Service/ServiceName.asmx/method?query=data1&count=1" 

在浏览器中完美运行。

请帮忙。

1 个答案:

答案 0 :(得分:0)

使用相对路径:

with(new XMLHttpRequest)
  {
  open("GET","/Service/ServiceName.asmx/method?query=data1&count=1",true);
  setRequestHeader("Foo", "Bar");
  send("");
  onreadystatechange = handler;
  }

function handler(event)
 {
 !!event.target && !!event.target.readyState && event.target.readyState === 4 && ( console.log(event) );
 }

如果这不起作用,请尝试从JavaScript加载URL以检查路由问题:

 window.location = "/Service/ServiceName.asmx/method?query=data1&count=1"