我正在開始使用ajax。有人可以幫我理解爲什麼ajax send()不更新地址欄嗎?連接起作用,但總是打印「沒有變量!」 PS:請記住,我不想使用JQuery。ajax send()不更新地址欄
TEST.HTM
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="posts"></div>
<script type="text/javascript">
var addressVariables = "sendvariable=test";
var hr = new XMLHttpRequest();
var url = "phpAjax.php";
hr.open("GET", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if (hr.readyState == 4 && hr.status == 200) {
var returnData = hr.responseText;
document.getElementById("centreSection").innerHTML = returnData;
}
};
hr.send(addressVariables);
</script>
<section id="centreSection"></section>
</body>
</html>
phpAjax.php
<?php
if (isset($_GET['sendvariable'])){
$testVar = $_GET['sendvariable'];
echo "Ajax rocks !";
echo $testVar;
} else {
echo "There is no variable!";
}
CSS.cutter非常感謝它的工作原理。 – Swartz