日期:2014-05-16 浏览次数:20492 次
function HttpRequest(sUrl,fpCallback)
{
this.request = this.createXmlHttpRequest();
this.request.open("GET",sUrl,true);
var tempRequest = this.request;
function request_readystatechange()
{
if (tempRequest.readyState == 4)
{
if (tempRequest.status == 200)
{
fpCallback(tempRequest.responseText);
}
else if (tempRequest.status == 0)
{
alert("An error occurred trying to contact the server.");
}
else
{
alert("错误");
}
}
}
this.request.onreadystatechange = request_readystatechange;
}
HttpRequest.prototype.createXmlHttpRequest = function ()
{
if (window.XMLHttpRequest)
{
var oHttp = new XMLHttpRequest();
return oHttp;
}
else if (window.ActiveXObject)
{
var versions =
[
"MSXML2.XmlHttp.6.0",
"MSXML2.XmlHttp.3.0"
];
for (var i = 0; 1 < versions.length; i++)
{
try
{
var oHttp = new ActiveXObject (versions[i]);
return oHttp;
}
catch (error)
{
//do nothing here
}
}
}
return null;
}
HttpRequest.prototype.send = function ()
{
this.request.send(null);
}
<html>
<head>
<script type="text/javascript" src="httprequest.js"></script>
</head>
<body>
<script type="text/javascript">
function handleData(sResponseText)
{
alert(sResponseText);
}
var url = "http://localhost/test1/111.php?userName=" + "aaa";
var request = new HttpRequest(url,handleData);
request.send();
</script>
</body>
</html>
<?php
$a = $_GET[userName];
return $a;
?>