1

我有 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 响应。当我手动输入 URL 时,我什至得到响应。

我应该如何调试它?

编辑

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

第一部分是

HTTP/1.1 200 正常\r\n

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

HTTP 分块响应

这说内容编码的实体主体(gzip)

下一部分和最后一部分是:

基于行的文本数据:text/html

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

<div> some text </div>
4

1 回答 1

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);
    }
}
于 2013-04-27T13:12:27.753 回答