AjaxE.html代码:
<!DOCTYPE html>
<html>
<head>
<title>ajax</title>
<meta charset ="utf-8">
</head>
<body>
<input type="button" value="Make Call" id="btn">
<script type="text/javascript" src="AjaxE.js"></script>
</body>
</html>
AjaxE.js代码
function getXMLHttpRequest()
{
try{
try{
return new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
return new ActiveXObject("Msxm12.XMLHTTP");
}
}
catch(e)
{
return new XMLHttpRequest();
}
}
function requestGet(url,query,req)
{
var myRandom =parseInt(Math.random()*9999999) ;
if(query =="")
{
var callurl = url+"?rand="+myRandom;
}
else{
var callurl = url +"?"+query+"&rand="+myRandom;
}
req.open("GET",callurl,true);
req.send(null);
}
function requestPost(url,query,req)
{
req.open("POST",url,true);
req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
req.send(query);
}
function doAjax(url,query,callback,reqtype,getxml)
{
var myreq = getXMLHttpRequest();
myreq.onreadystatechange= function()
{
if(myreq.readyState ==4)
{
if(myreq.status ==200)
{
var item =myreq.responseText;
if(getxml==1)
{
item = myreq.responseXML;
}
eval(callback+"(item)");
}
}
}
if(reqtype.toUpperCase() =="POST")
{
requestPost(url,query,myreq);
}
else{
requestGet(url,query,myreq);
}
}
function cback(text)
{
alert(text);
}
window.onload = function()
{
document.getElementById("btn").onclick = function()
{
doAjax("libtest.php","param = hello","cback","GET",0);
}
}
libtest.php代码
<?php echo "Parameter value was:".$_GET["param"]; ?>
在tomcat上运行,没有报错但是直接显示出了。。libtest.php里的代码