Code
<script src="ajax.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
function Comment()
{
var username=document.getElementById("username").value;
var url="comment.aspx?username="+username";
var content=document.getElementById("content").value;
var date="content="+escape(content);
var req=new WebRequest(url);
req.Method = "POST";
req.SetRequestHeader("Content-Length", date.length);
req.SetRequestHeader("charset", "utf-8");
req.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.onReady = function()
{
location.reload();
}
req.Send(date);
}
</script>
<script src="ajax.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
function Comment()
{
var username=document.getElementById("username").value;
var url="comment.aspx?username="+username";
var content=document.getElementById("content").value;
var date="content="+escape(content);
var req=new WebRequest(url);
req.Method = "POST";
req.SetRequestHeader("Content-Length", date.length);
req.SetRequestHeader("charset", "utf-8");
req.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.onReady = function()
{
location.reload();
}
req.Send(date);
}
</script>
这是在静态页面加上的JS代码,再在静态页的提交按纽写一个onclick事件就行.类似:
comment.aspx只是执行服务器的代码,把用户发表的评论写进数据库。这个具体的代码就不给出了。
ajax.js代码如下:
Code
function WebRequest(url)
{
this.Method = "GET";
this.Url = url;
this.Async = true;
this.Refresh = false;
this.onReady = null;
this.onLoad = null;
this.onError = null;
this.responseText = null;
this.responseXml = null;
var req = GetRequest();
var headers = new Array();
function GetRequest()
{
var req = null;
if ( window.XMLHttpRequest )
{
req = new XMLHttpRequest();
}
else if ( window.ActiveXObject )
{
try
{
req = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
req = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return req;
}
this.SetRequestHeader = function(name, value)
{
for ( var i = 0, j = headers.length; i < j; i++ )
{
if ( headers[i].Name == name )
{
headers[i] = {Name:name, Value:value};
return;
}
}
var _header = {Name:name, Value:value};
headers.push(_header);
}
this.Send = function(obj)
{
if ( req == null )
{
alert("Can't create object.");
return;
}
if ( this.Async )
{
var ohttp = this;
req.onreadystatechange = function()
{
if ( req.readyState == 4 )
{
if ( req.status == 200 )
{
ohttp.responseText = req.responseText;
ohttp.responseXml = req.responseXML;
if ( ohttp.onReady )
ohttp.onReady();
}
else
alert("Failed: " + req.status);
}
else
{
if ( ohttp.onLoad )
ohttp.onLoad();
}
}
}
try
{
var rnd = "";
if ( this.Refresh )
{
rnd = "webrequestrnd=" + Math.random();
if ( this.Url.indexOf("?") >= 0 )
rnd = "&" + rnd;
else
rnd = "?" + rnd;
}
req.open(this.Method, this.Url + rnd, this.Async);
for ( var i = 0, j = headers.length; i < j; i++ )
{
var header = headers[i];
req.setRequestHeader(header.Name, header.Value);
}
req.send(obj);
if ( !this.Async )
{
this.responseText = req.responseText;
this.responseXml = req.responseXML;
}
}
catch(err)
{
if ( this.onError )
this.onError(err);
else
alert("Failed: " + err.message);
}
}
this.Abort = function()
{
if ( req != null ) req.abort();
}
}
function ResponseReader(response)
{
/*
是否有错误(- 无错误1 - 有错误)
返回信息
返回结构
*/
this.hasError = false;
this.errorMessage = "";
this.responseBody = null;
this.responseText = "";
if ( response != null )
{
var errcode, message, rbody, bodytext;
try
{
var resobj = response.getElementsByTagName("response")[0];
errcode = resobj.getElementsByTagName("error")[0].firstChild.nodeValue * 1;
message = (resobj.getElementsByTagName("message")[0].firstChild) ? resobj.getElementsByTagName("message")[0].firstChild.nodeValue : "";
rbody = resobj.getElementsByTagName("responsebody")[0].firstChild;
bodytext = (rbody) ? rbody.nodeValue : "";
this.hasError = (errcode != 0);
this.errorMessage = message;
this.responseBody = rbody;
this.responseText = bodytext;
}
catch(err)
{
this.hasError = true;
this.errorMessage = "返回信息格式错误";
this.responseBody = null;
this.responseText = "";
}
}
}
function WebRequest(url)
{
this.Method = "GET";
this.Url = url;
this.Async = true;
this.Refresh = false;
this.onReady = null;
this.onLoad = null;
this.onError = null;
this.responseText = null;
this.responseXml = null;
var req = GetRequest();
var headers = new Array();
function GetRequest()
{
var req = null;
if ( window.XMLHttpRequest )
{
req = new XMLHttpRequest();
}
else if ( window.ActiveXObject )
{
try
{
req = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
req = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return req;
}
this.SetRequestHeader = function(name, value)
{
for ( var i = 0, j = headers.length; i < j; i++ )
{
if ( headers[i].Name == name )
{
headers[i] = {Name:name, Value:value};
return;
}
}
var _header = {Name:name, Value:value};
headers.push(_header);
}
this.Send = function(obj)
{
if ( req == null )
{
alert("Can't create object.");
return;
}
if ( this.Async )
{
var ohttp = this;
req.onreadystatechange = function()
{
if ( req.readyState == 4 )
{
if ( req.status == 200 )
{
ohttp.responseText = req.responseText;
ohttp.responseXml = req.responseXML;
if ( ohttp.onReady )
ohttp.onReady();
}
else
alert("Failed: " + req.status);
}
else
{
if ( ohttp.onLoad )
ohttp.onLoad();
}
}
}
try
{
var rnd = "";
if ( this.Refresh )
{
rnd = "webrequestrnd=" + Math.random();
if ( this.Url.indexOf("?") >= 0 )
rnd = "&" + rnd;
else
rnd = "?" + rnd;
}
req.open(this.Method, this.Url + rnd, this.Async);
for ( var i = 0, j = headers.length; i < j; i++ )
{
var header = headers[i];
req.setRequestHeader(header.Name, header.Value);
}
req.send(obj);
if ( !this.Async )
{
this.responseText = req.responseText;
this.responseXml = req.responseXML;
}
}
catch(err)
{
if ( this.onError )
this.onError(err);
else
alert("Failed: " + err.message);
}
}
this.Abort = function()
{
if ( req != null ) req.abort();
}
}
function ResponseReader(response)
{
/*
*/
this.hasError = false;
this.errorMessage = "";
this.responseBody = null;
this.responseText = "";
if ( response != null )
{
var errcode, message, rbody, bodytext;
try
{
var resobj = response.getElementsByTagName("response")[0];
errcode = resobj.getElementsByTagName("error")[0].firstChild.nodeValue * 1;
message = (resobj.getElementsByTagName("message")[0].firstChild) ? resobj.getElementsByTagName("message")[0].firstChild.nodeValue : "";
rbody = resobj.getElementsByTagName("responsebody")[0].firstChild;
bodytext = (rbody) ? rbody.nodeValue : "";
this.hasError = (errcode != 0);
this.errorMessage = message;
this.responseBody = rbody;
this.responseText = bodytext;
}
catch(err)
{
this.hasError = true;
this.errorMessage = "返回信息格式错误";
this.responseBody = null;
this.responseText = "";
}
}
}