You can save the following code in a file such as: Myajax.js, later in the project if you feel that jquery is very heavy, you can use your own AJAX library, do not worry about performance and compatibility!
/**
* Create an AJAX Request object
* @returns XMLHttpRequest
*/
function Createajaxobj () {
var req;
if (window. XMLHttpRequest) {
Req=new XMLHttpRequest ();
}else{
Req=new ActiveXObject ("msxml2.xmlhttp");
}
return req;
}
/**
* Send AJAX requests
* @param method Get/post
* @param URL Request path
* @param params parameter list format a=?&&b=?
* @param async True Async false Synchronous
* @param handle200 to handle successful functions
* @param loading processing functions in the load
* @param handle404 processing functions that cannot find an address
* @param handle500 processing Server Internal Error function
*/
function Sendajaxreq (method,url,params,async,handle200,loading,handle404,handle500) {
var req = Createajaxobj ();
Req.onreadystatechange = function () {
if (4==req.readystate) {
if (200==req.status) {
if (handle200) {
handle200 (Req.responsetext);
}
}else if (404==req.status) {
if (handle404) {
Handle404 ();
}
}else if (500==req.status) {
if (handle500) {
Handle500 ();
}
}
}else{
if (loading) {
Loading ();
}
}
};
if ("Get" ==method.tolowercase ()) {
Req.open (method,url+ (params==null? "": "?") +params), async);
Req.send (NULL);
}else if ("Post" ==method.tolowercase ()) {
Req.open (Method,url,async);
Req.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Req.send (params);
}
}
/** for Example
Send a request using your own Ajax class library
Sendajaxreq ("Post",
"${base}/brandajax/checkbrandid.do",
"Brandid=" +yhdbrandid,true,
Function (req) {
Eval ("var data=" + req.responsetext);
if (data.message==true) {
Alert ("Brand ID:" +yhdbrandid+ "already exists, cannot be added again!");
$ ("#hidBrandId") [0].innerhtml=];
$ ("#brandName"). Val ("");
}else{
submitflag=1;
$ ("#selBrand") [0].style.display = "none";
}
});
*/
Get rid of jquery and implement AJAX functions with your own JS library