xmlHttp每次都返回null

时间:2013-04-27 13:06:58

标签: javascript xmlhttprequest

我有java脚本ajax代码,那些是:

var xmlHttp = new GetXmlHttpObject();

function answer()
{

if(xmlHttp.readyState==4)
    {
        alert("response is given");
        alert(xmlHttp.responseXML);
        document.getElementById("bl").innerHTML =xmlHttp.responseText;
        randNum();
    }
else{
        document.getElementById("bl").innerHTML='<img src="so/loading.gif">';
    }
}

function sendCode()
{
var param="";
var sender_name = document.getElementById("sender_name").value;
var sender_emaile = document.getElementById("sender_emaile").value;
var subject = document.getElementById("subject").value;
var message = document.getElementById("message").value;
var security_code=document.feedback.security_code.value;
var security_code_id=document.feedback.security_code_id.value;
var responsibility = 123;

if(sender_name=="" || sender_emaile=="" || subject=="" || message=="" || security_code=="")
{ 

}else{
    param += "sender_name="+sender_name+"&sender_emaile="+sender_emaile+"&subject="+subject+"&message="+message+"&security_code="+security_code+"&responsibility="+responsibility+"&security_code_id="+security_code_id;
    xmlHttp.open("POST","http://www.acn.org/modules.php?name=Feedback&action=response",true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", param.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.onreadystatechange=answer;
    xmlHttp.send(param);
    }
}

但我的问题是xmlHttp.responseXML返回Null,但我可以在Wiresahrk(数据包嗅探器)中看到http响应。我手动输入网址时甚至得到回复。

我该如何调试?

被修改

Wireshark数据包包含很多我的服务器/客户端属性,但我可以说超文本传输​​协议包含两部分,后面有一部分,

第一部分是

  

HTTP / 1.1 200 OK \ r \ n

包含请求标头,第二部分是

  

HTTP分块响应

其中说内容编码实体主体(gzip)

下一节和最后一节是:

  

行基文本数据:text / html

包含我想要的响应,但它没有通过xmlHttp.responseXML或xmlHttp.responseText显示。

<div> some text </div>

1 个答案:

答案 0 :(得分:0)

问题可能在于您正在使用全局xmlhttp对象。

尝试

function answer(xmlHttp)
{

    if(xmlHttp.readyState==4)
    {
        alert("response is given");
        alert(xmlHttp.responseXML);
        document.getElementById("bl").innerHTML =xmlHttp.responseText;
        randNum();
    }
    else{
        document.getElementById("bl").innerHTML='<img src="so/loading.gif">';
    }
}

function sendCode()
{
    var param="";
    var sender_name = document.getElementById("sender_name").value;
    var sender_emaile = document.getElementById("sender_emaile").value;
    var subject = document.getElementById("subject").value;
    var message = document.getElementById("message").value;
    var security_code=document.feedback.security_code.value;
    var security_code_id=document.feedback.security_code_id.value;
    var responsibility = 123;

    if(sender_name=="" || sender_emaile=="" || subject=="" || message=="" || security_code=="")
    { 

    }else{
        var xmlHttp = new GetXmlHttpObject();

        param += "sender_name="+sender_name+"&sender_emaile="+sender_emaile+"&subject="+subject+"&message="+message+"&security_code="+security_code+"&responsibility="+responsibility+"&security_code_id="+security_code_id;
        xmlHttp.open("POST","http://www.acn.org/modules.php?name=Feedback&action=response",true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", param.length);
        xmlHttp.setRequestHeader("Connection", "close");
        xmlHttp.onreadystatechange=function(){
            answer(xmlHttp);
        };
        xmlHttp.send(param);
    }
}