我怎样才能得到一个Ajax请求的响应?Ajax

2023-09-10 20:50:58 作者:联合国认证小可爱

我想这code:

var xmlHttp = new XMLHttpRequest();

function activecomm(comm_id,a_link_id)
{
    var postComm = "id="+encodeURIComponent(comm_id);
    var url = 'comments_mgr_proccesser.php'; 
    xmlHttp.open("POST", url, true);
    xmlHttp.onreadystatechange = handleInfo(a_link_id);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", postComm.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(postComm);
}

function handleInfo(a_link_id)
{
    if(xmlHttp.readyState == 1)
    {
        document.getElementById("commactiveresult").innerHTML = 'loading ..';
    }
    else if(xmlHttp.readyState == 4)
    {
        var response = xmlHttp.responseText;
        document.getElementById("commactiveresult").innerHTML = response;
    }
}

的readyState == 1 commactiveresult 元素的内容进行更新,但是当的readyState == 4 没有显示在相同的元素。

When readyState == 1 the contents of the commactiveresult element is updated, but when readyState == 4 nothing is shown in the same element.

有谁知道是什么问题吗?

Does anyone know what the problem is please?

推荐答案

您正在呼叫的 handleInfo 函数,而不是分配就绪状态的处理程序。尝试

You're calling the handleInfo function instead of assigning a ready state handler. Try

xmlHttp.onreadystatechange = function (){
    handleInfo(a_link_id);
};