【问题标题】:XMLHttpRequest responseXML is always nullXMLHttpRequest responseXML 始终为空
【发布时间】:2013-07-10 21:44:03
【问题描述】:

我正在调用这样的 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" 

在浏览器中完美运行。

请帮忙。

【问题讨论】:

  • 而且服务当然在同一个域中。
  • 我改了实现CORS的代码,但还是同样的问题...
  • 我无法更改服务器端代码,需要进行哪些更改才能进行跨源调用...
  • Java 组件、AIR 或瘦客户端怎么样?

标签: javascript ajax xml web-services debugging


【解决方案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"

【讨论】:

    猜你喜欢
    • 2012-12-29
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 2015-09-05
    • 2017-06-17
    相关资源
    最近更新 更多