简体   繁体   English

如何获得Ajax请求的响应?

[英]How can I get the response from an Ajax request?

I tried this 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;
    }
}

When readyState == 1 the contents of the commactiveresult element is updated, but when readyState == 4 nothing is shown in the same element. readyState == 1时, commactiveresult元素的内容被更新,但是当readyState == 4时,同一元素中未显示任何内容。

Does anyone know what the problem is please? 有人知道是什么问题吗?

You're calling the handleInfo function instead of assigning a ready state handler. 您正在调用handleInfo函数,而不是分配就绪状态处理程序。 Try 尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM