我正在尝试对 Ajax 进行一个非常简单的测试 - 更改标题。但是,不是用 DoSomthing.php 文件中的所需文本更改标题,而是用我的所有 main.php 文件内容修改标题。
主文件是一个php文件,但为了测试,不需要php代码。
<!DOCTYPE html>
<html>
<head>
<script>
//-----------------------------------------------------------
function myFunction2()
{
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("Title").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","DoSomething.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<p id="Title">What do you say? :)</p>
<button type="button" onclick="myFunction2()">Try Ajax</button>
</body>
</html>
新标题由与主 php 文件位于同一文件夹中的 php 文件给出。这是 Ajax 调用的用于修改标题的 DoSomthing.php 文件。
<?php
echo "Hellooo there!";
?>
顺便说一句,调试控制台中没有报告错误。
所以我不明白这里发生了什么,也没有在网上发现任何类似的问题。
谢谢