Get rid of jquery and implement AJAX functions with your own JS library

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.