0

只是试图制作一个非常简单的 AJAX,但什么也没有。谢谢!

<script>
function update()
{

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

  var str='test';

xmlhttp.open("GET","localhost/userupdate.php?q="+str,true);
xmlhttp.send();
}
</script>

和 php 页面 localhost/userupdate.php (WAMP)

<?php
$q=$_GET["q"];
echo $q;
?>
4

1 回答 1

4

首先,您应该在xmlhttp.open--中使用完整的 URL http://localhost/userupdate.php。如果该userupdate.php文件与此脚本位于同一目录中,那么您可以简单地使用userupdate.php

其次,您没有对响应做任何事情。由于响应是一个字符串,您可以使用responseText属性来检索它。

function update() {

    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            console.log(xmlhttp.responseText);
        }
    }

    var str = 'test';

    xmlhttp.open("GET", "http://localhost/userupdate.php?q=" + str, true);
    xmlhttp.send();
}
于 2013-10-08T16:40:39.360 回答