封装一个简单的Ajax函数
/**
* Ajax操作函数
*
* @param url -- 服务器端页面地址
* @param param -- 参数,类似 'user=123&id=100'
* @param method -- 请求服务器端的方法,Get和Post两种,默认是GET
* @param response -- 是否获取服务器端返回的结果,默认是true
*/
function ajax( url, param, method, response ){
//set default value
param = typeof(param)=='undefined' ? '' : param;
method = typeof(method)=='undefined' ? 'GET' : method;
response = typeof(response)=='undefined' ? true : response;
//get ajax object
var ajax = null;
try {
ajax = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
ajax = null;
}
}
if (!ajax && typeof XMLHttpRequest!='undefined'){
ajax = new XMLHttpRequest();
}
if (!ajax){
alert("Get ajax object failed");
return false;
}
//send http request
var res = '';
if (method != 'GET'){
method = 'POST';
}
if (method == 'GET'){
ajax.open('GET', url + param, true);
ajax.onreadystatechange = function(){
if (ajax.readyState==4 && ajax.status==200){
res = ajax.responseText;
}
}
ajax.send(null);
}
if (method == "POST"){
ajax.open("POST", url, true);
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.send(param);
ajax.onreadystatechange = function() {
if (ajax.readyState==4 && ajax.status==200) {
res = ajax.responseText;
}
}
}
if (response){
return res;
}
return null;
}
相关文档:
Download board.zip - 39.6 KB - Old Version
Download board_2008.zip - 55.03 KB - Latest VS 2008 Version
Introduction
This is an AJAX based WhiteBoard application. Typically unlike their desktop based counterparts, web applications need to be designed to use optimal ......
上传页面只需要一个js引用和js函数的调用,简单方便,不影响网页原有布局。
下载地址
调用说明:
1.首先引用js文件
<script type="text/javascript" src="AienUpload/init.js"></script>
2.将函数调用(showUpload(boxCtrl,inputCtrl,upPath,maxCount,fn);)绑定到HTMl控件事件,例如
......
===============下骗Ajax
================
十一、第十一课 ===》 使用XHR对象发送和接受数据
a.继续上面步骤
2)注册回调函数,只写函数名称(如果加了括号,那么就把函数返回值注册给回调函数)
xmlHttp.onreadystatechange = callback;
3)设置连接信息
//第一参数是Http请求方式,一般选择get、pos ......
[AJAX介绍]
Ajax是使用客户端脚本与Web服务器交换数据的Web应用开发方法。Web页面不用打断交互流程进行重新加裁,就可以动态地更新。使用Ajax,用户可以创建接近本地桌面应用的直接、高可用、更丰富、更动态的Web用户界面。
异步JavaScript和XML(AJAX)不是什么新技术,而是使用几种现有技术——包 ......