不管是post还是get都无法拿到返回值,status要么是0要么是200.
请求状态变更的时候明明有拿到数据。。。但是最后又没有了。。(如下截图)
也不知道为什么。。cgi应该木有问题,我试过直接访问的。
js相关代码如下,求大神解答!
function createXHR()
{
var xhr;
try
{
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(E)
{
xhr = false;
}
}
if (!xhr && typeof XMLHttpRequest != 'undefined')
{
xhr = new XMLHttpRequest();
}
return xhr;
}
function computor()
{
xhr = createXHR();
var operand1=document.getElementById('operand1').value;
var operand2=document.getElementById('operand2').value;
var operator=document.getElementById('operator').value;
var datas = "operand1="+operand1+"&operator="+operator+"&operand2="+operand2;
if(xhr)
{
xhr.onreadystatechange=callbackFunction;
xhr.open("GET", "./compute.cgi?operand1="+operand1+"&operator="+operator+"&operand2="+operand2,true);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.setRequestHeader("If-Modified-Since","0"); //防止ie缓存
xhr.send(datas);
}
else
{
//XMLHttpRequest对象创建失败
alert("浏览器不支持,请更换浏览器!");
}
}
function callbackFunction()
{
if (xhr.readyState == 4)
{
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304)
{
var returnValue = xhr.responseText;
if(returnValue != null && returnValue.length > 0)
{
document.getElementById("result").innerHTML = returnValue;
}
else
{
alert("结果为空!");
}
}
else
{
alert("页面出现异常!");
}
}
}