XMLHttpRequest 的空响应

Ros*_*e18 3 javascript ajax xmlhttprequest

在我的 Java Web 应用程序中,我调用 ajax 请求,如下所示。

<script type="text/javascript">
function selectTableHandler() {
    console.log("indise selectTableHandler");
    var xhttp = new XMLHttpRequest();

    var selectedTable = document.getElementById('selectTable').value;
    alert(selectedTable);
    xhttp.open("GET", "populateTableColumList.action?tablename="
            + selectedTable, true);
    xhttp.send();
    console.log(xhttp.responseText);
}
</script>
Run Code Online (Sandbox Code Playgroud)

该操作正在调用并返回状态代码 200,如下所示。

Remote Address:[::1]:8080
Request        URL:http://localhost:8080/ReportBuilder/populateTableColumList.action?tablename=films
Request Method:GET
Status Code:200 OK
Run Code Online (Sandbox Code Playgroud)

但是,它给出了 XMLHttpRequest 的空响应。该行console.log(xhttp.responseText); 不打印任何内容。另外,控制台日志中也没有错误。

任何建议,将不胜感激。

谢谢

Gay*_*ith 5

您需要添加一个回调函数来获取ajax请求的结果。

xhttp.onreadystatechange = function() {
  if (xhttp.readyState == 4 && xhttp.status == 200) {
    console.log(xhttp.responseText);
  }
}
Run Code Online (Sandbox Code Playgroud)