您的位置:首页 > 编程语言

ajax公用代码备份

2008-08-25 15:23 190 查看
做ajax的时候经常用的公用代码在此备份:
ajax.js

function newXMLHttpRequest() {
var reqHttp;
if (window.ActiveXObject) { // IE
try {
reqHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
reqHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e1) {
reqHttp = null;
}
}
} else if (window.XMLHttpRequest){ // IE ??
try {
reqHttp = new XMLHttpRequest();
} catch (e) {
reqHttp = null;
}
}
if (reqHttp == null) errorMessage(); //XMLHttpRequest ?? ??
return reqHttp;
}

function errorMessage() {
alert("error");
}

function openSendStatus(getPost, urlFileAppl, trueFalse, sendData, flag) {
var xmlHttp = newXMLHttpRequest();
xmlHttp.open(getPost, urlFileAppl, trueFalse);
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
mainControl(xmlHttp, flag);
} else {
exceptionControl(xmlHttp);
}
}
}
var conType = "application/x-www-form-urlencoded; charset=UTF-8";
xmlHttp.setRequestHeader("Content-Type", conType);
xmlHttp.send(sendData);
}

function exceptionControl(xmlHttp) {
var exceptShow = "error code: " + xmlHttp.status;
exceptShow += ", closed";
alert(exceptShow);
}

应用实例:

function mainControl(xmlHttp) {
var sourceData = decodeURIComponent(xmlHttp.responseText);
var setdata = eval("("+sourceData+")");
if(setdata==false){
alert("The notice title has been existed!/n please reset title!");
MAIN_FORM.NOTICE_TITLE.value = "";
MAIN_FORM.NOTICE_TITLE.focus();
return ;
}
}

function validateNotice(){
var hidden_name = MAIN_FORM.hidden_name.value;
var idx_id = MAIN_FORM.IDXID.value;
var notice_name = MAIN_FORM.NOTICE_TITLE.value;
if(hidden_name!=notice_name){
var getPost = "POST";
var urlFileAppl = "ES0116_02_Action.jsp";
var trueFalse = true;
var sendData = "idx_id=" + idx_id+"&ACTION=VALIDATE¬ice_name="+notice_name;
openSendStatus(getPost, urlFileAppl, trueFalse, sendData);
}
}

仅供个人使用参考
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: