javascript - XMLHttpRequest 响应XML 始终为 null

标签 javascript ajax xml web-services debugging

我正在调用这样的 asmx Web 服务

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" 

在浏览器中完美运行。

请帮忙。

最佳答案

使用相对路径:

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"

关于javascript - XMLHttpRequest 响应XML 始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17581434/

相关文章:

java - GWT - 从 context.xml 加载配置项

xml - 在不编码 HTML 实体的情况下进行 XML 编码

c++ - 从 xsd 生成 xml 文件的工具(用于测试)

javascript - 在Extjs源码中定义var Ext

javascript - Chrome 触发滚动事件

ajax - grails3 Ajax请求在渲染为JSON后中断

Javascript同步加载表文件

javascript - 将行添加到 Google 数据可视化表

javascript - 'Jeditable' 请求后重新加载页面

php - 如何多次加载AJAX来分别更改页面(页码)?