ajax测试例子(get方式)

来源:互联网 发布:汉王ocr文字识别软件 编辑:程序博客网 时间:2024/05/16 09:35

html文件(test.html):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>XMLHttpRequest</title>
<script language="javascript">
var xmlHttp;
function GetXmlHttpRequest()
{
var xmlHttp=null;
try
{
xmlHttp = new XMLHttpRequest(); //对于Firefox等浏览器
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //对于IE浏览器
}
catch (e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
xmlHttp = false;
}
}
}
return xmlHttp;
}
function startRequest(){
 var xmlHttp=GetXmlHttpRequest();
 var sUrl = "ajax.php?"+ new Date().getTime(); //地址不断的变化
 xmlHttp.open("GET",sUrl,true);
 xmlHttp.onreadystatechange = function(){
  if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
   alert("服务器返回: " + xmlHttp.responseText);
 }
 xmlHttp.send(null);
}
</script>
</head>
<body>
<input type="button" value="测试异步通讯" onClick="startRequest()">
</body>
</html>

 

php文件(ajax.php):

<?php

echo iconv("GB2312","UTF-8",'hello 中国');
//echo "hello 中国";


?>

 

 

原创粉丝点击