运行ajax需要用到apache服务器,选用的是apache2.2,服务器安装完后的文件目录如下图所示:
[img]http://dl.iteye.com/upload/attachment/343638/04809bda-9a2d-31f6-ac2f-fdba0f161cb3.jpg[/img]
其中的htdocs目录存放的是平时用的html,php文件
HelloWorld。html文件
hello_world.php
启动apache服务器后,在浏览器中输入http://127.0.0.1/HelloWorld.html
会弹出一个hello world对话框
[img]http://dl.iteye.com/upload/attachment/343638/04809bda-9a2d-31f6-ac2f-fdba0f161cb3.jpg[/img]
其中的htdocs目录存放的是平时用的html,php文件
HelloWorld。html文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hello World</title>
<script type="text/javascript">
try
{
var xmlhttp = new XMLHttpRequest();
}
catch(e)
{
var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.open('GET','hello_world.php',true);//打开php文件
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
alert(xmlhttp.responseText);
}
}
xmlhttp.send();
</script>
</head>
<body>
</body>
</html>
hello_world.php
<?
echo 'Hello World';
?>
启动apache服务器后,在浏览器中输入http://127.0.0.1/HelloWorld.html
会弹出一个hello world对话框