Simple implementation of native AJAX encapsulation and ajax Encapsulation

Source: Internet
Author: User

Simple implementation of native AJAX encapsulation and ajax Encapsulation

Returning to native js, the AJAX package seen on the Internet is used for modification. I don't know what else the disadvantages are. I hope to point out it!

Var ajaxHelper = {/* 1.0 create an asynchronous object using browser compatibility */makeXHR: function () {// declare the asynchronous object variable var xmlHttp = false; // declare the extension name var xmlHttpObj = ["MSXML2.XMLHttp. 5.0 "," MSXML2.XMLHttp. 4.0 "," MSXML2.XMLHttp. 3.0 "," MSXML2.XMLHttp "," MSXML. XMLHttp "]; // determines whether the browser supports XMLHttpRequest. if the browser supports XMLHttpRequest, it is a new browser. You can directly create if (window. XMLHttpRequest) {xmlHttp = new XMLHttpRequest ();} // otherwise, you can only traverse the asynchronous Object Name of the old browser cyclically and try to create the object. else if (window. activeXObjec T) {for (I = 0; I <xmlHttpObj. length; I ++) {xmlHttp = new ActiveXObject (xmlHttpObj [I]); if (xmlHttp) {break ;}}// determines whether the asynchronous object is created successfully. if yes, otherwise, return false return xmlHttp? XmlHttp: false;},/* 2.0 sends an Ajax Request */doAjax: function (method, url, data, isAyn, callback, type) {method = method. toLowerCase (); // 2.1 create an asynchronous object var xhr = this. makeXHR (); // 2.2 sets the request parameter (if it is get, it carries the url parameter, if not, it does not) xhr. open (method, url + (method = "get "? "? "+ Data:" "), isAyn); // 2.3 add different request headers (if (method =" get ") {xhr. setRequestHeader ("If-Modified-Since", 0);} else {xhr. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");} // 2.4 sets the callback function xhr. onreadystatechange = function () {// if the response packet sent from the server is accepted, if (xhr. readyState = 4) {// judge whether the status code is normal if (xhr. status = 200) {if (type. toLowerCase () = "json") {var ret = {}; try {If (typeof JSON! = "Undefined") {ret = JSON. parse (xhr. responseText);} The following else {// IE8 does not support JSON ret = new Function ("return" + xhr. responseText) ();} callback (ret);} catch (e) {console. log (e. message); callback (false) ;}} else {// directly return the text callback (xhr. responseText) ;}} else {console. log ("AJAX Status Code:" + xhr. status); callback (false) ;}}; // send 2.5 (if it is post, the parameter is passed; otherwise, the parameter is not passed) xhr. send (method! = "Get "? Data: null) ;},/* 3.0 directly sends the Post Request */doPost: function (url, data, isAyn, callback, type) {this. doAjax ("post", url, data, isAyn, callback, type) ;},/* 4.0 directly sends a Get request */doGet: function (url, data, isAyn, callback, type) {this. doAjax ("get", url, data, isAyn, callback, type );}};

Assume that the backend needs to input two numbers n1 and n2, and then return the sum.

If one of the parameters is null or not a number, {"status": "0", "msg": "parameter error is returned! "}

If yes, return: {"status": "1", "sum": "// n1 + n2 and "}

The back-end code will not be pasted out.

Frontend call:

Document. getElementById ("btnSubmit "). onclick = function () {ajaxHelper. doPost ("backend url", "n1 = 10 & n2 = 25", true, function (ret) {if (! Ret) {return;} if (ret. status! = 1) {alert (ret. msg); return;} var n = ret. sum; var s = ret. status ;}, "json ");};

The simple implementation of the above native AJAX package is all the content shared by the editor. I hope to give you a reference, and I hope you can also support the help house.

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.