本案例包含4部分:(1)HTML部分;(2)ajax部分;(3)JavaScript部分;(4)node服务器部分。另外,因为牵涉到服务器,所以这里没法“效果预览”。
执行过程为:
(1)在浏览器地址栏输入网址,向node服务器发送HTML请求;服务器接到请求后,返回一个HTML文件给客户端;
(2)客户端浏览器对HTML进行渲染,遇到<script>标签后,再次向服务器请求,服务器响应一个JavaScript文件给客户端,
(3)客户端浏览器对JavaScript文件进行渲染,渲染过程中,如果遇到ajax请求,客户端还会向服务器进行请求,服务器仍然会响应浏览器。
(4)最后,浏览器把渲染好的网页呈现在浏览者面前。
1、HTML部分:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>客户管理系统</title> </head> <body> <div> <a href="javascript:void(0);">NEW CUSTOMER</a> <h2> <span>ID</span> <span>NAME</span> <span>AGE</span> <span>PHONE</span> <span>ADDRESS</span> <span>CONTROL</span> </h2> <ul> <li> <span>1</span> <span>钱成</span> <span>25</span> <span>13044086186</span> <span>Bei Jing</span> <span> <a href="javascript:void(0);" >修改</a> <a href="javascript:void(0);" >删除</a> </span> </li> </ul> </div> <script charset="utf-8" type="text/javascript" src="https://www.jb51.net/js/ajax.js"></script> <script charset="utf-8" type="text/javascript" src="https://www.jb51.net/js/index.js"></script> </body> </html>
2、ajax部分:
~function () { //->createXHR:创建AJAX对象,兼容所有的浏览器 function createXHR() { var xhr = null, flag = false, ary = [ function () { return new XMLHttpRequest; }, function () { return new ActiveXObject("Microsoft.XMLHTTP"); }, function () { return new ActiveXObject("Msxml2.XMLHTTP"); }, function () { return new ActiveXObject("Msxml3.XMLHTTP"); } ]; for (var i = 0, len = ary.length; i < len; i++) { var curFn = ary[i]; try { xhr = curFn(); createXHR = curFn; flag = true; break; } catch (e) { } } if (!flag) { throw new Error("your browser is not support ajax,please change your browser,try again!"); } return xhr; } //->ajax:实现AJAX请求的公共方法; function ajax(options) { var _default = { url: "", type: "get", dataType: "json", async: true, data: null, getHead: null, success: null }; for (var key in options) { if (options.hasOwnProperty(key)) { _default[key] = options[key]; } } if (_default.type === "get") { _default.url.indexOf("?") >= 0 ? _default.url += "&" : _default.url += "?"; _default.url += "_=" + Math.random(); } //->SEND AJAX var xhr = createXHR(); xhr.open(_default.type, _default.url, _default.async); xhr.onreadystatechange = function () { if (/^2\d{2}$/.test(xhr.status)) { if (xhr.readyState === 2) { if (typeof _default.getHead === "function") { _default.getHead.call(xhr); } } if (xhr.readyState === 4) { var val = xhr.responseText; if (_default.dataType === "json") { val = "JSON" in window ? JSON.parse(val) : eval("(" + val + ")"); } _default.success && _default.success.call(xhr, val); } } }; xhr.send(_default.data); } window.ajax = ajax; }();
3、JavaScript部分:
var customer = (function () { var conList = document.getElementById('conList'); function bindEvent() { conList.onclick = function (ev) { ev = ev || window.event; var tar = ev.target || ev.srcElement, tarTag = tar.tagName.toUpperCase(), tarInn = tar.innerHTML; if (tarTag === 'A' && tarInn === '删除') { //->ALERT、CONFIRM、PROMPT var cusId = tar.getAttribute('data-id'), flag = window.confirm('确定要删除编号为 [ ' + cusId + ' ] 的客户吗?'); if (flag) {//->点击的是确定按钮:调取对应的API接口实现删除即可 ajax({ url: '/removeInfo?id=' + cusId, cache: false, success: function (result) { if (result && result.code == 0) { //->删除成功后在HTML结构中移除对应的LI标签 conList.removeChild(tar.parentNode.parentNode); } } }); } } } } function bindHTML(data) { var str = ''; for (var i = 0; i < data.length; i++) { var cur = data[i]; str += '<li>'; str += '<span>' + cur.id + '</span>'; str += '<span>' + cur.name + '</span>'; str += '<span>' + cur.age + '</span>'; str += '<span>' + cur.phone + '</span>'; str += '<span>' + cur.address + '</span>'; str += '<span>'; str += '<a href="add.html?id=' + cur.id + '" >修改</a>'; str += '<a href="javascript:;" data-id="' + cur.id + '">删除</a>'; str += '</span>'; str += '</li>'; } conList.innerHTML = str; } return { init: function () { ajax({ url: '/getAllList', type: 'GET', dataType: 'JSON', cache: false, success: function (result) { if (result && result.code == 0) { bindHTML(result.data); bindEvent(); } } }); } } })(); customer.init();
4、node服务器部分: