Chinaunix首页 | 论坛 | 博客
  • 博客访问: 295474
  • 博文数量: 153
  • 博客积分: 3347
  • 博客等级: 中校
  • 技术积分: 1556
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-30 17:50
文章分类

全部博文(153)

文章存档

2013年(7)

2012年(21)

2011年(46)

2010年(16)

2009年(63)

我的朋友

分类: 系统运维

2009-12-30 18:14:08

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    处理页面的地址,比如:
*@ 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


ajax.php
header("Content-Type:gb2312");
echo "how areyou?";
if(isset($_POST["name"]))
{
echo "hello,".$_POST["name"];
}
?>
阅读(215) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~