JavaScript之Ajax(一)创建Ajax对象

时间:2021-05-12 17:46:40
// 支持浏览器的源码
function AjaxObject() {
var AjaxRequest; // 缓存XHR对象便于 Ajax 使用 try {
// Opera 8.0+, Firefox, Safari
AjaxRequest = new XMLHttpRequest();
} catch (e) { // Internet Explorer Browsers
try {
AjaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
AjaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// 错误处理
throw new Error("Your browser broke!");
return false;
}
}
} return AjaxRequest;
}

  测试函数:

function validateUserId() {
AjaxFunction(); // 这里的 processRequest() 就是回调函数
AjaxRequest.onreadystatechange = processRequest; if (!target) target = document.getElementById("userid");
var url = "validate?id=" + escape(target.value);
alert(unescape(target.value)); //test:解码
AjaxRequest.open("GET", url, true);
AjaxRequest.send(null);
} function processRequest(req) {
if (req.readyState == ) {
if (req.status == ) {
var message = "Hello world";
alert(message);
setMessageUsingDOM(message);
//...
}
}
}
//引文:http://caibaojian.com/ajax-jsonp.html
ajax({
url: "./TestXHR.aspx", //请求地址
type: "POST", //请求方式
data: { name: "super", age: 20 }, //请求参数
dataType: "json",
success: function (response, xml) {
// 此处放成功后执行的代码
},
fail: function (status) {
// 此处放失败后执行的代码
}
}); function ajax(options) {
options = options || {};
options.type = (options.type || "GET").toUpperCase();
options.dataType = options.dataType || "json";
var params = formatParams(options.data); //创建 - 非IE6 - 第一步
if (window.XMLHttpRequest) {
var xhr = new XMLHttpRequest();
} else { //IE6及其以下版本浏览器
var xhr = new ActiveXObject('Microsoft.XMLHTTP');
} //接收 - 第三步
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
var status = xhr.status;
if (status >= 200 && status < 300) {
options.success && options.success(xhr.responseText, xhr.responseXML);
} else {
options.fail && options.fail(status);
}
}
} //连接 和 发送 - 第二步
if (options.type == "GET") {
xhr.open("GET", options.url + "?" + params, true);
xhr.send(null);
} else if (options.type == "POST") {
xhr.open("POST", options.url, true);
//设置表单提交时的内容类型
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(params);
}
}
//格式化参数
function formatParams(data) {
var arr = [];
for (var name in data) {
arr.push(encodeURIComponent(name) + "=" + encodeURIComponent(data[name]));
}
arr.push(("v=" + Math.random()).replace(".",""));
return arr.join("&");
}

  

function ajax(src,data_,method){
//创建并调用ajax
htmlobj = $.ajax({
url : src,
asyc : false,
type : method,
data : data_,//发送到服务器的数据。将自动转换为请求字符串格式。GET 请求中将附加在 URL 后。查看 processData 选项说明以禁止此自动转换。【Eg:{ "id": "value" }, //参数值】
dataType : "json",//预期服务器返回的数据类型。如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息返回 responseXML 或 responseText,并作为回调函数参数传递
contentType:"application/x-www-form-urlencoded",
complete : function(data) {
try{
// alert("ajax:data:"+data);//test
var jsonData = eval("(" + data.responseText + ")");
}catch(error){
$("#ajax_info").css({"padding":"10px","bakcground":"#ccc","color":"red","font-size":"1.6em"}); $("#ajax_info").html(data.responseText);
// alert(error.message);
$("h3.box-title").css({"background-color":"white","color":"#ccc"});//防止上述文档改变部分的页面样式
return;
} // alert(jsonData.msg);
if(jsonData.msg=="OK"){//如果数据库中删除成功
//删除tr文本节点
alert(jsonData.msg_cont);
}else{
alert(jsonData.msg_cont);
}
}
});//htmlobj----end
}