使用XMLHttpRequest从另一个页面获取数据

使用XMLHttpRequest从另一个页面获取数据,xmlhttprequest,Xmlhttprequest,大家早上好。我尝试创建一个可以从另一个页面获取信息并将响应反馈到div标记中的站点,但没有成功。我和我从中学到的一个非常相似的例子进行了核对。下面是HTML代码 <html> <head> <script> function checkit(){ var samxml; var username; if (window.XMLHttpRequest){ samxml = new XMLHttpRequest();} else { samxml = new

大家早上好。我尝试创建一个可以从另一个页面获取信息并将响应反馈到div标记中的站点,但没有成功。我和我从中学到的一个非常相似的例子进行了核对。下面是HTML代码

<html>
<head>

<script>
function checkit(){
var samxml; var username;
if (window.XMLHttpRequest){
samxml = new XMLHttpRequest();}
else  {
samxml = new ActiveXObject("Microsoft.XMLHTTP");
}

samxml.onreadystatechange = function() {

if (samxml.readyState == 4 && samxml.status == 200){
document.getElementById("check").innerHTML = samxml.responseText;}
}
username = document.getElementById("username").value;
samxml.open("POST", "check.php",true);

samxml.send( "username" );



}
}

</script>
</head>
<body>
<form>
<label for ="username">User ID:</label>
<input type = "text" name = "username" id = "username"/>


<div id = "check"></div>
<button type = "button" onclick = "checkit()">Check</button>
</form>




</body>
</html>
以下是PHP页面:

//php file
<?php
$user = $_POST["username"];
if ($user == "samuel") {
echo "Hmm. Chosen! Choose another one";}
else {
echo "Nice one";}
?>

非常感谢。

浏览器控制台中是否有错误?你能看到正在发送的AJAX请求吗?我能看到UrL的变化。其中一个错误是php脚本中的我的变量没有声明或其他什么。