|
| 1 | +;(function(window, document, undefined) { |
| 2 | + function Ajax(opts) { |
| 3 | + |
| 4 | + opts.type = opts.type || 'get'; |
| 5 | + opts.type = opts.type.toLowerCase(); |
| 6 | + opts.dataType = opts.dataType || 'json'; |
| 7 | + opts.dataType = opts.dataType.toLowerCase(); |
| 8 | + |
| 9 | + if (opts.dataType == 'jsonp') { |
| 10 | + jsonpRequest(opts); |
| 11 | + return; |
| 12 | + } |
| 13 | + |
| 14 | + var xhr = new XMLHttpRequest(), |
| 15 | + params = null; |
| 16 | + |
| 17 | + xhr.onreadystatechange = function() { |
| 18 | + if (xhr.readyState == 4) { |
| 19 | + opts.done && opts.done(jsonParse(xhr.responseText), xhr.responseXML); |
| 20 | + if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) { |
| 21 | + opts.success && opts.success(jsonParse(xhr.responseText), xhr.responseXML); |
| 22 | + } else { |
| 23 | + opts.fail && opts.fail(jsonParse(xhr.responseText), xhr.responseXML); |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + if (opts.type == 'get') { |
| 29 | + params = formatParams(opts.data); |
| 30 | + var url = opts.url.indexOf('?') > -1 ? (opts.url + '&' + params) : (opts.url + '?' + params); |
| 31 | + xhr.open('get', url, true); |
| 32 | + opts.headers && setHeaders(); |
| 33 | + xhr.send(null); |
| 34 | + } else if (opts.type == 'post') { |
| 35 | + params = formatParams(opts.data); |
| 36 | + xhr.open('post', opts.url, true); |
| 37 | + xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); |
| 38 | + opts.headers && setHeaders(); |
| 39 | + xhr.send(params); |
| 40 | + } |
| 41 | + |
| 42 | + function formatParams(data) { |
| 43 | + var arr = []; |
| 44 | + for (var key in data) { |
| 45 | + arr.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key])); |
| 46 | + } |
| 47 | + return arr.join('&'); |
| 48 | + } |
| 49 | + |
| 50 | + function jsonpRequest(opts) { |
| 51 | + |
| 52 | + if (!opts.url) { |
| 53 | + console.error('url missing'); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + opts.jsonpCallback = opts.jsonpCallback || 'callback'; |
| 58 | + |
| 59 | + var callbackName = 'jsonp_' + Math.ceil((Math.random() * 1E12)); |
| 60 | + |
| 61 | + opts.data[opts.jsonpCallback] = callbackName; |
| 62 | + |
| 63 | + //创建script标签 |
| 64 | + var params = formatParams(opts.data), |
| 65 | + oHead = document.querySelector('head'), |
| 66 | + oScript = document.createElement('script'); |
| 67 | + oHead.appendChild(oScript); |
| 68 | + |
| 69 | + //创建回调函数 |
| 70 | + window[callbackName] = function(json) { |
| 71 | + oHead.removeChild(oScript); |
| 72 | + window[callbackName] = null; |
| 73 | + window.clearTimeout(oScript.timer); |
| 74 | + opts.success && opts.success(json); |
| 75 | + opts.done && opts.done(json); |
| 76 | + }; |
| 77 | + |
| 78 | + //发起请求 |
| 79 | + oScript.src = opts.url + '?' + params; |
| 80 | + |
| 81 | + if (opts.time) { |
| 82 | + oScript.timer = window.setTimeout(function() { |
| 83 | + oHead.removeChild(oScript); |
| 84 | + window[callbackName] = null; |
| 85 | + opts.fail && opts.fail({message: 'timeout'}); |
| 86 | + opts.done && opts.done({message: 'timeout'}); |
| 87 | + }, opts.time); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + function setHeaders() { |
| 92 | + for (var o in opts.headers) { |
| 93 | + // console.log(o, opts.headers[o]); |
| 94 | + xhr.setRequestHeader(o, opts.headers[o]); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + function jsonParse(text) { |
| 99 | + return JSON.parse(text); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + window.Ajax = Ajax; |
| 104 | + |
| 105 | +})(window, document); |
| 106 | + |
| 107 | +if (typeof module !== 'undefined') { |
| 108 | + module.exports = window.Ajax; |
| 109 | +} else if (typeof define === 'function' && define.amd) { |
| 110 | + define([], function () { |
| 111 | + 'use strict'; |
| 112 | + return window.Ajax; |
| 113 | + }); |
| 114 | +} |
0 commit comments