Compile your own javascript function library-Ajax (like jquery) and ajaxjquery. Compile your own javascript function library Ajax (like jquery). ajaxjquery learns php, so it uses php and js to demonstrate the code, the main objective is to develop your own js writing skills, to train your hands and write your own javascript function library Ajax (like jquery) and ajaxjquery.
I learned php, so I used php and js to demonstrate the code, mainly to train myself in writing JavaScript and to train my hands.
Below is the code function I wrote to operate on ajax, so I can barely call it a library ..
Js code instance (tool. ajax. js ):
1/** 2 * JS library use ajax 3 * @ author jlb 4 */5 if (typeof tool = 'undefined') {6 var tool = function (){}; 7} 8 tool. ajax = function () {}; 9 10 11/** 12 * get ajax object 13 * @ return success return ajax object 14 */15 tool. ajax. getAjaxObject = function () {16 try {return new XMLHttpRequest ()} catch (e) {} 17 try {return new ActiveXOject ('Microsoft. XMLHTTP ')} catch (e) {} 18 alert ('Your browser version is too low! Please upgrade your browser '); 19} 20 21 22/** 23 * ajax submit data 24 * @ param parameter list 25 * @ return void26 */27 tool. ajax. formSubmit = function (options) {28 var allow_param, // list of allowed parameters 29 HTTP, // 30 url of ajax object, // 31 data of the request address; // carry the data 32 33 allow_param = ['method', 'URL', 'data', 'success ', 'type']; 34 // set the default value 35 if (! Options ['type']) {36 options ['type'] = 'text'; 37} 38 39 // process url and data, merge data with URL 40 var disposeParam = function (list) {41 var data = {url: list ['URL'], data :''}; 42 if (list ['method'] = 'get') {43 data ['data'] + = '? '; 44 for (var I in list ['data']) {45 data ['data'] + = I + '=' + list ['data'] [I] + '&'; 46} 47} 48 if (list ['method'] = 'post') {49 for (var I in list ['data']) {50 data ['data'] + = I + '=' + list ['data'] [I] + '&'; 51} 52} 53 return data54} 55 data = disposeParam (options); 56 HTTP = tool. ajax. getAjaxObject (); 57 // ajax callback function 58 HTTP. onreadystatechange = function () {59 if (HTTP. readyState = 4 & HTTP. status = 200) {60 if (options ['type'] = 'text') {61 options ['success'] (HTTP. responseText); 62} 63 else if (options ['type'] = 'json') {64 options ['success '] (eval (' + HTTP. responseText + '); 65} 66} 67} 68 69 if (options ['method'] = 'get ') {70 url = data ['URL'] + data ['data']; 71 HTTP. open (options ['method'], url); 72 // Set the request header to solve the cache issue in get submission. fix the issue by modifying the last modification time of the file 73 HTTP. setRequestHeader ('if-Modified-Since ', 0); 74 HTTP. send (null); 75 return; 76} 77 78 if (options ['method'] = 'post') {79 HTTP. open (options ['method'], data ['URL']); 80 // Set the request header to 81 HTTP. setRequestHeader ('content-type', 'application/x-www-form-urlencoded'); 82 HTTP. send (data ['data']. replace (/(& * $)/g, ''); 83 return; 84} 85}
Use export (ajax_test.html ):
1234
5Simple ajax function library usage example678
9102627
Script Code of the ajax request (ajax_test.php ):
1
Open the ajax_test.html file in the browser, and the browser displays:
Name: Mo Wen source, Age: 20
If the returned data is in json format, change the value of the type attribute in option to json.
If you have any questions, please comment on me. I was a little excited when I first wrote a blog. this Cainiao took the first step.
Callback (like jquery), ajaxjquery learns php, so it uses php and js to demonstrate the code, mainly to train itself to write JavaScript, just to train hands...