简体   繁体   中英

XMLHttpRequest responseXML is always null

I am calling a asmx web service like this

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

even after the readystate being 4, I get responseXML as null and responseText as empty. whereas the url

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

works perfectly in the browser.

Please help.

Use a relative path:

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

If that doesn't work, try loading the URL from JavaScript to check for routing issues:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM