AJAX不能与XAMPP一起工作,或者这是不可能的



我对AJAX还是比较陌生的,我正在努力弄清楚为什么我的简单测试不起作用。据我所知,AJAX不会在一个域下工作,其他词会像跨站点和远程服务器一样弹出。无论如何,我的问题是,我不确定我的代码是否错误,或者我想做的事情是不可能的。我创建了一个简单的ajax请求,以便在单击按钮时提交数据。这是第一个脚本的代码。

<html>
  <head>
    <script type="text/javascript" >
      function load(thediv, thefile) {
        if (window.XMLHttpRequest) {
          xmlhttp = new XMLHttpRequest();   
        } 
        else {
          xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        }   
        xmlhttp.onreadystatechange = function() {
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById(thediv).innerHTML = xmlhttp.responseText;
          }     
        }
        xmlhttp.open('GET', thefile, true);
        xmlhttp.send();
      }
    </script>
  </head>
  <body>
    <input type="submit" onclick="load('adiv', 'hello.php');">
    <div id="adiv"></div>
  </body>
</html>

以下是hello.php文件的代码

<?php
  echo 'aaa';
?>

AJAX只是一个在后台完成的http请求。但是,确实存在一些安全限制,阻止您对任意服务器进行正常的ajax请求。

您的代码所缺少的实际上是设置请求所指向的URL。您已经将hello.php作为load()函数的参数,但您从未真正USE它。因此,您正在执行AJAX请求…"nothing"。

相关内容

最新更新