如何从Ajax请求中获取响应?

时间:2013-03-22 16:38:33

标签: javascript ajax xmlhttprequest

我试过这段代码:

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时,同一元素中不会显示任何内容。

有谁知道问题是什么?

1 个答案:

答案 0 :(得分:1)

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

xmlHttp.onreadystatechange = function (){
    handleInfo(a_link_id);
};
相关问题