/**
ajax_frame.js
Ajax Frame Version 1.0
*/
function ajax_frame()
{
var config = new Array();
var httpobj = null;
config['transfer_mode'] = 'GET';
config['async'] = true;
/* 设置传输模式 */
this.set_transfer_mode = function()
{
if ('GET' == arguments[0].toUpperCase() || 'POST' == arguments[0].toUpperCase())
{
config['transfer_mode'] = arguments[0].toUpperCase();
}
}// end set_transfer_mode;
this.set_async = function()
{
if ('boolean' == typeof arguments[0])
{
config['async'] = arguments[0];
}
}// end set_async;
/*********************************************
* 函数名称:call_func
* arguments[0] 目标文件的url
* arguments[1] 调用的函数
* arguments[2] 响应函数
* arguments[3] url要传递的对象1
* .
* .
* .
* arguments[n+3] url要传递的对象n
************************************************/
this.call_func = function()
{
if ('function' == typeof arguments[2])
{
client_callback = arguments[2];
}
get_connection_object();
var i;
var query_string = "?function=" + arguments[1];
//if ('' == arguments[1])
//query_string = "?function=" + arguments[1];
var field = null;
for(i = 3; i < arguments.length; i++)
{
field = arguments[i];
query_string = query_string + '&' + field.name + '=' + field.value;
}
/* 把时间戳加入url为的是保持url唯一性,避免浏览器缓存结果 */
var remote_url = arguments[0] + query_string + '&timeStamp=' + new Date().getTime();
httpobj.open(config['transfer_mode'], remote_url, config['async']);
if ("POST" == config['transfer_mode'])
{
try {
httpobj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
} catch (cp_err) {
alert('[错误] 无法发送POST请求,请使用GET方式发送');
}
}
httpobj.onreadystatechange = callback;
httpobj.setRequestHeader("Cache-Control","no-cache");
httpobj.setRequestHeader("Pragma","no-cache");
if ('GET' == config['transfer_mode'])
{
httpobj.send(null);
}
else
{
httpobj.send(query_string);
}
if (false == config['async'])
{
callback();
}
}//end call_func
var callback = function()
{
var response = null;
if (4 == httpobj.readyState)
{
if (200 == httpobj.status)
{
response = httpobj.responseText;
if (response != null && typeof client_callback == 'function')
{
client_callback(response, httpobj.responseText);
}
}
}
}// end of callback
/****************************************
* 得到XMLHTTP连接对象
*****************************************/
var get_connection_object = function()
{
try {
httpobj = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e)
{
try {
httpobj = new ActiveXObject('Microsoft.XMLHTTP');
} catch (oc) {
httpobj = null;
}
}
if (!httpobj && typeof XMLHttpRequest != 'undefined') {
httpobj = new XMLHttpRequest();
}
if (!httpobj) {
alert('[错误] 浏览器不支持AJAX对象');
} else {
return_value = true;
}
if (httpobj.readyState != 4)
{
httpobj.abort();
}
return return_value;
}// end of get_connection_object
}
使用方式:
sample.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=gb2312" />
<script language="javascript" src="js/ajax_frame.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
window.onload = function()
{
var button = document.getElementById('say');
button.onclick = function()
{
var obj = new ajax_frame;
var txt = document.getElementById('person');
obj.call_func("helloworld.php", "helloworld", response_func, txt);
}
}
function response_func(result)
{
alert(result);
}
</script>
<title>无标题文档</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<input type="text" name="person" id="person" />
<input type="button" name="say" id="say" value="Say Hello Word" />
</form>
</body>
</html>
helloworld.php
<?php
$func = $_GET['function'];
$uid = $_POST['txtUID'];
$pwd = $_POST['txtPWD'];
if (function_exists($func))
$func($uid, $pwd);
else
echo "0";
function helloworld()
{
echo $_GET["person"] . " say hello world!";
}
?>
目前还没有支持xml,以后会加上。