AJAX不使用XAMPP或者它是不可能的

时间:2012-07-19 21:45:19

标签: ajax

我还是比较新的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';
?>

1 个答案:

答案 0 :(得分:1)

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

您的代码缺少的是实际设置您要发出请求的网址。你有hello.php作为load()函数的参数,但实际上你从未 USE 。所以你正在做一个AJAX请求......“无处”。