创建AJAX对象
function createAjax(){
/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttp = false;
}
}
@end @*/
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
//alert(xmlHttp);
return xmlHttp;
}
应用
function callServer(workNo){
xmlHttp = null;
xmlHttp = createAjax();
// Build the URL to connect to
var url = "ReferenceWorkNo?flg=checkWorkNo&workNo="+toUN.on(workNo);
var d = new Date();
var rdm = d.getTime();
url = url+'&rdm='+rdm;
//url=encodeURI(url);
//url=encodeURI(url); //写一个不行。如果写一个就是????号。
// Open a connection to the server
//xmlHttp.abort();
xmlHttp.open("GET", url, true);//true is asyn
// Setup a function for the server to run when it's done
xmlHttp.onreadystatechange = dealWorkNoResult;
// Send the request
xmlHttp.send(null);
}
function dealWorkNoResult(){
if (xmlHttp.readyState == 4) {
var response = xmlHttp.responseText;
//var strResult = unescape(response);
var rest = parseInt(response);
if(rest>0){
alert("您输入的工号已经存在,请重新输入");
var workNo = document.getElementById('workNo');
workNo.value='';
workNo.focus();
xmlHttp = null;
return;
}
}
}
另附应用于JS的unicode的转码与解码
var toUN = {
on: function(str) {
var a = [],
i = 0;
for (; i < str.length;) a[i] = ("00" + str.charCodeAt(i++).toString(16)).slice( - 4);
return "\\u" + a.join("\\u")
},
un: function(str) {
return unescape(str.replace(/\\/g, "%"))
}
};