0
나는 PHP와 AJAX를 사용하여 페이지에 게시물을 작성하고 있습니다. 아래는 페이지의 자바 스크립트 코드입니다.PHP AJAX 응답 없음
function Post(posted_by, posted_to)
{
document.getElementById('post_textarea').disabled='disabled';
document.getElementById('post_button').style.display='none';
document.getElementById('loader').style.display='inline';
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttpPost=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttpPost=new ActiveXObject("Microsoft.XMLHTTP");
}
var post=document.getElementById("post_textarea").value;
var params="type=post&posted_by=" + posted_by + "&posted_to=" + posted_to + "&post=" + post //Parameters for post method..
xmlhttpPost.open("POST","test.php",true);
//Send headers; data sent as if it has been posted from form
xmlhttpPost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttpPost.setRequestHeader("Content-length", params.length);
xmlhttpPost.setRequestHeader("Connection", "close");
xmlhttpPost.onreadystatechange=function()
{
//I can reach here...
if (xmlhttpPost.readyState==4 && xmlhttpPost.status==200)
{
//BUT I CAN'T REACH HERE.. GETTING NO RESPONSE
document.getElementById('posts').innerHTML = xmlhttpPost.responseText + document.getElementById('posts').innerHTML;
document.getElementById("post_textarea").value=""; //Clear textbox
hidePostBox();
}
}
xmlhttpPost.send(params); //Send POST DATA to the server..
}
PHP 파일에서 작동하는지 테스트하기 위해 텍스트 행을 에코했습니다.
하지만 나는 그 텍스트 줄을 repsonse로 가져 오지 않습니다. 전혀 응답이 없습니다.
다른 웹 사이트에서이 동일한 자바 스크립트 코드를 사용하고 있었고 작동했습니다. 그리고 여기 같은 코드가 작동하지 않습니다.
무엇이 결함일까요 ??
오류 콘솔 (Firebug 또는 Chrome Dev Tools)을 확인 했습니까? – jmdeldin
페이지에서 일부'div (s)가 누락 된 것 같습니다. 요소가 누락되지 않았는지 확인하십시오. else 코드는 내가 조사한대로 잘 작동합니다. –
아마도 getElementById()를 사용할 때 이것이 실제로 NULL을 반환하지 않는지 확인해야합니까? – Yang