整理一个自己用的Ajax例子,封装,调用!
什么是Ajax?
也就是javascript可以及时向服务器提出请求和处理响应,而不阻塞用户。达到无刷新的效果。
核心机制为:XMLHttpRequest
XMLHttpRequest属性:
onreadystatechange 每次状态改变所触发事件的事件处理程序。
responseText 从服务器进程返回数据的字符串形式。
responseXML 从服务器进程返回的DOM兼容的文档数据对象。
status 从服务器返回的数字代码,比如常见的404(未找到)和200(已就绪)
status Text 伴随状态码的字符串信息
readyState 对象状态值,0—未初始化 1—正在加载 2—加载完毕 3—交互 4—完成。
JS:
function Ajax() {
var xmlHttpReq = null;
if (window.XMLHttpRequest) {
xmlHttpReq = new XMLHttpRequest();
} else {
if (window.ActiveXObject) {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
}
var handler = null;
this.invoke = function (url, mode, synchro, _handler) {
handler = _handler;
xmlHttpReq.open(mode, url, synchro);
xmlHttpReq.onreadystatechange = this.callback;
xmlHttpReq.send(null);
};
this.callback = function () {
if (xmlHttpReq.readyState == 4) {
if (xmlHttpReq.status == 200) {
handler(xmlHttpReq.responseText);
} else {
alert("There was a problem retrieving the XML data:\n" + xmlHttpReq.statusText);
}
}
};
}
var xmlHttpReq = null;
if (window.XMLHttpRequest) {
xmlHttpReq = new XMLHttpRequest();
} else {
if (window.ActiveXObject) {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
}
var handler = null;
this.invoke = function (url, mode, synchro, _handler) {
handler = _handler;
xmlHttpReq.open(mode, url, synchro);
xmlHttpReq.onreadystatechange = this.callback;
xmlHttpReq.send(null);
};
this.callback = function () {
if (xmlHttpReq.readyState == 4) {
if (xmlHttpReq.status == 200) {
handler(xmlHttpReq.responseText);
} else {
alert("There was a problem retrieving the XML data:\n" + xmlHttpReq.statusText);
}
}
};
}
调用方式:
var ajax = new Ajax();
ajax.invoke("http://www.xx.com/getlist.aspx?type=1&id=1","GET",true,function(response){
var json = eval("(" + response + ")");
});
ajax.invoke("http://www.xx.com/getlist.aspx?type=1&id=1","GET",true,function(response){
var json = eval("(" + response + ")");
});
Jquery方式:
//参数设置,设置的格式为key:value,如果{"cl":"check","dd":"dd"},获取的格式为cl=check&dd=dd
var params = $.param({"il":Math.random()});
//验证是否登录
$.ajax({
type:"POST",
url:"Control/Login.aspx",
data:encodeURI(params),
success:function(response){
var json = eval("("+response+")");
//操作JSON,json[0];json.Table[0].id;
}
});
//---------------
$.getJSON("Control/GetData.aspx",{t:Math.random()},function(json){
alert(json.ok);
if(json.ok==true){
alert(json.Table[0].ID);
}
});
//遍历JSON
$.each( { name: "John", lang: "JS" }, function(i, n){
alert( "Name: " + i + ", Value: " + n );
});
var params = $.param({"il":Math.random()});
//验证是否登录
$.ajax({
type:"POST",
url:"Control/Login.aspx",
data:encodeURI(params),
success:function(response){
var json = eval("("+response+")");
//操作JSON,json[0];json.Table[0].id;
}
});
//---------------
$.getJSON("Control/GetData.aspx",{t:Math.random()},function(json){
alert(json.ok);
if(json.ok==true){
alert(json.Table[0].ID);
}
});
//遍历JSON
$.each( { name: "John", lang: "JS" }, function(i, n){
alert( "Name: " + i + ", Value: " + n );
});
专注iOS、Golang开发。
技术博客:http://xiaopin.cnblogs.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?