ajax框架0.1版,用函数的方式,未封装(转载)

来源:互联网 发布:工字钢简支梁计算软件 编辑:程序博客网 时间:2024/05/18 00:57
ajax1.js

/*
使用方法:
ajax_get("url.php",回调函数);
回调函数中有一个参数是文本型的,是服务器返回来的,如果回调函数名是call_back,那么我们可以这样定义它:
function call_back(text_from_server)
{
   alert(text_from_server);
}
注意目前回调函数只有一个参数,参数名可以自定.
ajax_post("url.php","参数",回调函数);和ajax_get大同小异,不再介绍
*/
//--------------------------------------------------------------------框架开始
function getXmlHttpObject()
{
/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
   xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
   try {
   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (e2) {
   xmlHttp = false;
   }
}
@end @*/
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
   xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}
function ajax_get(url,callback)
{
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange =function(){ handleReadyState(callback); }
xmlHttp.send(null);
}
/**
*@ url    处理页面的地址,比如: http://localhost/post.php
*@ param   参数比如 name=yangQingRong&birth=1985&city=jieyang
*@ callback 回调函数的句柄 比如有一个函数function sayHello(name1){},那么我们填"sayHello"
*/
function ajax_post(url,param,callback)
{
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=function(){ handleReadyState(callback); }
xmlHttp.setRequestHeader("Content-Length",param.length);    
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");  
xmlHttp.send(param);
}
function handleReadyState(callback) {
if (xmlHttp.readyState == 4) {
    
    var response = xmlHttp.responseText;
callback(response);
}
}
var xmlHttp=getXmlHttpObject();
//--------------------------------------------------------------------框架结束
ajax.htm
<script language="javascript" src="ajax1.js"></script>
<script language="javascript">
ajax_get("ajax.php",back);
var name="杨庆荣!";
//name= escape(name);
ajax_post("ajax.php","name="+name,back2);
function back2(res)
{ alert(res);
}
function back(res)
{
alert(res);
}
</script>
ajax.php
<?
header("Content-Type:gb2312");
echo "how areyou?";
if(isset($_POST["name"]))
{
echo "hello,".$_POST["name"];
}
?>
阅读(97) | 评论(0) | 转发(0) |
0

上一篇:PHP 的表格 类 基本完成

下一篇:采集程序(1) 公用函数库

相关热门文章
  • javascript-数组
  • javascript-对象
  • 注册块设备的过程分析...
  • 赣州市成人高考作文掌握6点拿...
  • C++学习-继承中的作用域...
  • IP Sec VPN与NAT破镜重圆
  • 网站导航
  • UT2.0正式版下载
  • tomcat6.0配置(含配置视频下载...
  • Gomez中国网站用户体验排行榜(...
  • LNMP 老是会出现502?
  • suse 运用一个shell获取本机和...
  • 虚拟机 unix 配置ip
  • hp-un 主机新系统读不到磁盘阵...
  • mysql出现问题:Starting MySQ...
给主人留下些什么吧!~~