这是原生的ajax,稍稍的封装了下,
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
/*
调用方式:
1.POST方式
var txt = escape(sender.value); //document.getElementById("<%= txtName.ClientID %>").value);
var data = "name=" + txt + "&pwd=" + txt;
var option = { "url": "handler/Handler.ashx"
, "action": "POST"
, "callback": function(){
if (xmlHttp.readyState == 4) {//服务器给了回应
if (xmlHttp.status == 200) {//服务正确响应
alert(xmlHttp.responseText);
}
xmlHttp = null; //回收资源
}
}
, "data": data
};
ajax(option);
2.GET方式
var txt = escape(sender.value); //document.getElementById("<%= txtName.ClientID %>").value);
var option = { "url": "handler/Handler.ashx&name=" + txt + "&pwd=" + txt
, "action": "POST"
, "callback": function(){
if (xmlHttp.readyState == 4) {//服务器给了回应
if (xmlHttp.status == 200) {//服务正确响应
alert(xmlHttp.responseText);
}
xmlHttp = null; //回收资源
}
}
};
ajax(option);
*/
function ajax(option) {
createXMlHttpRequest(); // 创建xmlHttpRequest 对象
if (option != null && option != undefined) {
if (option.url == null && option.url == undefined) {
xmlHttp = null ;
alert( " 缺少必要参数option.url " );
return ;
}
if (option.action == null && option.action == undefined) {
xmlHttp = null ;
alert( " 缺少必要参数option.action " );
return ;
}
xmlHttp.open(option.action, option.url, true );
if (option.contentType != null && option.contentType != undefined) {
xmlHttp.setRequestHeader( " Content-Type " , option.contentType);
} else {
xmlHttp.setRequestHeader( " Content-Type " , " application/x-www-form-urlencoded " );
}
if (option.callback != null && option.callback != undefined) {
xmlHttp.onreadystatechange = option.callback;
}
if (option.action.toUpperCase() == " POST " ) {
xmlHttp.send(option.data);
} else {
xmlHttp.send( null );
}
}
}
var xmlHttp; // 调用完成后最好回收下 xmlHttp = null;
/* 获取元素 */
function g(arg) {
var t = document.getElementById(arg);
if ( null != t && t != undefined) {
return t;
}
t = document.getElementsByName(arg);
if ( null != t && t != undefined) {
return t;
}
t = document.getElementsByTagName(arg);
if ( null != t && t != undefined) {
return t;
}
}
/* 创建ajax请求对象 */
function createXMlHttpRequest() {
try { // Firefox, Chrome, Surfri, Opera+8
xmlHttp = new XMLHttpRequest();
}
catch (ie) {
try { // IE6+
xmlHttp = new ActiveXObject( " Msxml2.XMLHTTP " );
} catch (ie) {
xmlHttp = new ActiveXObject( " Microsoft.XMLHTTP " );
}
}
return xmlHttp;
}
调用方式:
1.POST方式
var txt = escape(sender.value); //document.getElementById("<%= txtName.ClientID %>").value);
var data = "name=" + txt + "&pwd=" + txt;
var option = { "url": "handler/Handler.ashx"
, "action": "POST"
, "callback": function(){
if (xmlHttp.readyState == 4) {//服务器给了回应
if (xmlHttp.status == 200) {//服务正确响应
alert(xmlHttp.responseText);
}
xmlHttp = null; //回收资源
}
}
, "data": data
};
ajax(option);
2.GET方式
var txt = escape(sender.value); //document.getElementById("<%= txtName.ClientID %>").value);
var option = { "url": "handler/Handler.ashx&name=" + txt + "&pwd=" + txt
, "action": "POST"
, "callback": function(){
if (xmlHttp.readyState == 4) {//服务器给了回应
if (xmlHttp.status == 200) {//服务正确响应
alert(xmlHttp.responseText);
}
xmlHttp = null; //回收资源
}
}
};
ajax(option);
*/
function ajax(option) {
createXMlHttpRequest(); // 创建xmlHttpRequest 对象
if (option != null && option != undefined) {
if (option.url == null && option.url == undefined) {
xmlHttp = null ;
alert( " 缺少必要参数option.url " );
return ;
}
if (option.action == null && option.action == undefined) {
xmlHttp = null ;
alert( " 缺少必要参数option.action " );
return ;
}
xmlHttp.open(option.action, option.url, true );
if (option.contentType != null && option.contentType != undefined) {
xmlHttp.setRequestHeader( " Content-Type " , option.contentType);
} else {
xmlHttp.setRequestHeader( " Content-Type " , " application/x-www-form-urlencoded " );
}
if (option.callback != null && option.callback != undefined) {
xmlHttp.onreadystatechange = option.callback;
}
if (option.action.toUpperCase() == " POST " ) {
xmlHttp.send(option.data);
} else {
xmlHttp.send( null );
}
}
}
var xmlHttp; // 调用完成后最好回收下 xmlHttp = null;
/* 获取元素 */
function g(arg) {
var t = document.getElementById(arg);
if ( null != t && t != undefined) {
return t;
}
t = document.getElementsByName(arg);
if ( null != t && t != undefined) {
return t;
}
t = document.getElementsByTagName(arg);
if ( null != t && t != undefined) {
return t;
}
}
/* 创建ajax请求对象 */
function createXMlHttpRequest() {
try { // Firefox, Chrome, Surfri, Opera+8
xmlHttp = new XMLHttpRequest();
}
catch (ie) {
try { // IE6+
xmlHttp = new ActiveXObject( " Msxml2.XMLHTTP " );
} catch (ie) {
xmlHttp = new ActiveXObject( " Microsoft.XMLHTTP " );
}
}
return xmlHttp;
}