【发布时间】:2013-05-02 15:20:07
【问题描述】:
<script>
try {
function xmldo() {
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("para").innerHTML = xmlhttp.responseText;
}
}
var URL = "http:\\127.0.0.1\ajax.php";
xmlhttp.open("GET", URL, true);
xmlhttp.send();
}
} catch (err) {
document.write(err.message);
}
</script>
<p id="para">Hey message will change</p>
<br>
<button type="button" onclick="xmldo()">Click me</button>
这是我的代码网页,我想通过我的另一个 php 文件 ajax.php 中的响应来更改#para.innerHTML 的内容
<?php
$response="hey is text changed";
echo $response;
?>
我正在使用 wamp,所以我将 ajax.php 放在我的 www 文件夹中,并将服务器上文件的位置设置为 127.0.0.1/ajax.php [URL] 但我按下按钮时 para 占位符处的文本不是改变。 我是 AJAX 的新手,所以在某些方面必须遗漏。请帮帮我。
【问题讨论】:
-
我建议你使用 jQuery 之类的库来执行 AJAX 请求 - 它的主要好处是它需要更少的代码并且可以在各种浏览器中使用。
-
您是否尝试将
"http:\\127.0.0.1\ajax.php"更改为"http://127.0.0.1/ajax.php"? -
@ajtrichards 我现在对 Jquery 一无所知,但非常感谢您的宝贵建议。
标签: php javascript xml ajax wamp