我遇到了一个奇怪的问题,我通过Ajax请求调用Java Servlet,当我同步调用它时,它工作得很好,但每当我从ajax向servlet发送异步请求时,ajax都不会收到响应。
Ajax代码:
.cpp
我的servlet:
<script type="text/javascript">
function ajaxSyncRequest(reqURL) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", reqURL, true); //when false here everything works
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send('server=test');
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
document.getElementById("1").innerHTML = xmlhttp.responseText;
} else {
document.getElementById("1").innerHTML = "Could not connect to server!";
}
}
setTimeout(function(){ ajaxSyncRequest("ConsoleGenerator"); }, 1500);
}
function init() {
ajaxSyncRequest("ConsoleGenerator");
}
init();
</script>";