2013-03-22 175 views
1

我试过这段代码:如何从Ajax请求获取响应?

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; 
    } 
} 

当​​的commactiveresult元素的内容进行更新,但如果在同一个元件示readyState == 4什么。

有谁知道这个问题是什么?

+0

是: ** ** – Shadoo77 2013-03-22 16:45:00

+0

这应该被编辑到问题中。 – Musa 2013-03-22 16:46:41

回答

1

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

xmlHttp.onreadystatechange = function(){ 
    handleInfo(a_link_id); 
}; 
在 'comments_mgr_proccesser.php'
+0

yaaaaaaaaaaaaaaah ...非常非常匹配 – Shadoo77 2013-03-22 16:48:15

+0

@ user2192164不要忘记标记为您的答案,如果它的工作。 – 2013-03-22 16:56:08

+0

确定maaaan谢谢 – Shadoo77 2013-03-22 17:02:09