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);
}
}
仅供个人使用参考
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);
}
}
仅供个人使用参考
相关文章推荐
- 20151212Jquery 工具函数代码备份
- C#备份收藏夹代码
- web服务文件更新自动同步、数据库主从复制、shell脚本实现网站代码备份和mysql备份
- 20151214 jquery插件代码备份
- stock代码备份
- 用批处理实现自动备份和清理mysql数据库的代码
- linux(CentOS) 下mysql与代码自动备份与清理
- 杂乱无章 代码备份
- 几个个实用的PHP代码片段【自己备份】
- linux php mysql数据库备份实现代码
- linux php mysql数据库备份实现代码
- 备份LeetCode OJ自己编写的代码
- 常用代码备份--多线程(二)
- 工作中的登录流程以及代码备份
- CTDB和SAMBA数据库备份代码分析
- 备份数据库用到的相关代码(在数据库事务中添加)
- Windows下实现Oracle数据库定时自动备份批处理代码
- 备份shell脚本实例代码
- 代码备份:动态维护半平面交/凸包
- Asp.net在线备份、压缩和修复Access数据库示例代码