如何在网络模拟中使用 AJAX,XAMPP

How do I use AJAX within a web simulation, XAMPP

我是 Ajax 的新手,但想学习如何使用它。我已经阅读了 Mozilla 提供的 "getting started" 部分,我正在关注它,但它无法在 onreadystatechange 上打开函数。

这是我的代码

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <form action=""> 
            Other info: <textarea cols=50 rows=10 
                                  onblur = "processText(this.value)"
                                  id='info1'></textarea>"
        </form>

        <script>
            function processText(str) {
                var xhttp;
                xhttp = new XMLHttpRequest();
                alert('After new XMLHttpRequest');
                alert('ready state 1' + xhttp.readyState);
                xhttp.onreadystatechange = function () {
                    alert('onready statechange function entered');
                };

            }
        </script>
    </body>
</html>

我已将其减少到最低限度以显示问题所在。当我 运行 时,我在框中输入一些文本,然后将光标移到外面,'onblur'。这会正确调用函数 'processText(str)' 并显示警报 'After new XMLHttpRequest'。下一个警报显示 ReadyState = 10(我认为只有 0 到 4 是可能的!)。警报 'onready statechange function entered' 未显示。

我在 Whosebug 上看到了信息 How to make AJAX work on local server using XAMPP or node.js

它提到可能需要设置端口。我正在使用端口 1337,所以我通过 'localhost:1337' 获得了 XAMPP 页面。但是我找不到如何设置端口,如果这是问题所在。

这是我的配置: XAMPP 版本 3.2.2 网豆 8.1 Internet 浏览器 11

Ajax 请求需要打开和发送方法。将这些行添加到您的示例中,您实际上执行了 ajax 请求。

function processText(str) {
  var xhttp = new XMLHttpRequest();
  alert('After new XMLHttpRequest');
  alert('ready state 1' + xhttp.readyState);
  xhttp.open('get', 'http://your.url/path:port'); // <- new
  // -> https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/open
  xhttp.onreadystatechange = function () {
    alert('onready statechange function entered');
  };
  xhttp.open(null); // <- new
  // -> https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send
}