Ajax通信技术是前端非常重要的技能之一,它的出现使网页的使用体验发生了翻天覆地的变化。其中GET和POST方法是Ajax的主流,而如DELETE,PATCH方法却少有人使用,虽然Jquery等库支持这些东西,但如果离开第三方库甚至有很多新手不懂怎么用。其实很简单,这里就封装一个可以实现这些方法的Ajax组件
此文所有源码地址
封装组件
实现一个组件,首先需要设计一个封装性还算靠谱的方案,闭包是主流选择。
1 2 3 4 5 6
| var _ = (function() { return { } })()
|
然后封装可能用到的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| getXhr: function() { if(!new XMLHttpRequest()) { XMLHttpRequest = function() { try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {} try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {} try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {} return false; } } return new XMLHttpRequest(); },
serialize: function(url,data) { if(!data) return url; for (var name in data) { if (!data.hasOwnProperty(name)) continue; if (typeof data[name] === 'function') continue; var value = data[name].toString(); name = encodeURIComponent(name); value = encodeURIComponent(value); url += (url.indexof("?") == -1 ? "?" : "&"); url += name + "=" + value; } return url; }
|
材料准备好了,开始上主菜
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| ajax: function(options) { var xhr = this.getXhr(); xhr.withCredentials = true; xhr.onreadystatechange = function() { if (xhr.readyState === 4) { if (xhr.status === 200) { options.callback(xhr.responseText); } else { if(options.error) options.error(xhr.responseText); console.log('错误代码:' + xhr.responseText); } } } if (options.method.toUpperCase() === 'GET' || 'DELETE') {
var url = this.serialize(options.url,options.data) xhr.open(options.method, url, true); this.setRequestHeader(xhr, options.header); xhr.send(null);
} else if (options.method.toUpperCase() === 'POST' || 'PATCH') {
xhr.open(options.method, options.url, true); this.setRequestHeader(xhr, options.header); xhr.send(JSON.stringify(options.data));
} else { console.log('不识别的方法:' + options.method); return fasle; } }
|
如上。
setRequestHeader比较常用,所以单独拎出来封装了:
1 2 3 4 5 6 7 8 9 10 11 12 13
| setRequestHeader: function(xhr, headers) { if(!headers) { xhr.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' ); return; } for (var name in headers) { xhr.setRequestHeader( name.toString(), headers[name].toString() ); } }
|
调用方法
调用方法如下,除了url和method的值是必选的,其它都是可选的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| _.ajax({ url: '/api/test', method: 'GET/POST/DELETE/PATCH', data: { name: 'xxx', id: 001 }, header: { contenttype: 'application/json' }, callback: function(data) { console.log('ok') }, error: function(data) { console.log('falser' + data) } })
|
总结
通过以上代码,可以看出各种方法其实都大同小异,但在这里我要特别说一下:
由于一些冷门方法不常用,因此可能很多人认为Ajax调用method时是不区分大小写的。但其实并不是的,比如PATCH方法
实现PATCH方法时一定要注意用大写
这也是今天用patch调用后端接口时发现的,由于之前全用的小写,所以一开始就没注意,导致连不上服务器。耗费了两个小时的时间找bug
因此我这里封装的方法中直接toUpperCase()转成了大写。