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