getData: function(){ var name, value; if (opts.data) { if (typeof opts.data === "string") { opts.data = opts.data.split("&"); for (var i = 0, len = opts.data.length; i < len; i++) { name = opts.data[i].split("=")[0]; value = opts.data[i].split("=")[1]; opts.data[i] = encodeURIComponent(name) + "=" + encodeURIComponent(value); } opts.data = opts.data.replace("/%20/g", "+"); } else if (typeof opts.data === "object") { var arr = []; for (var name in opts.data) { var value = opts.data[name].toString(); name = encodeURIComponent(name); value = encodeURIComponent(value); arr.push(name + "=" + value); } opts.data = arr.join("&").replace("/%20/g", "+"); } //使用GET方法或JSONP,則手動添加到URL中 if (opts.type === "GET" || opts.dataType === "jsonp") { opts.url += opts.url.indexOf("?") > -1 ? opts.data : "?" + opts.data; } } },
createJsonp: function(){ var script = document.createElement("script"), timeName = new Date().getTime() + Math.round(Math.random() * 1000), callback = "jsonp_" + name; window[callback] = function(data) { clearTimeout(ajax.options.timeoutFlag); document.body.removeChild(script); try { data && (data = JSON.parse(data)); } catch (e) { console.error('ajax error for json parse responseText'); } ajax.success(data); } script.src = url + (url.indexOf("?") > -1 ? "" : "?") + "callback=" + callback; script.type = "text/javascript"; document.body.appendChild(script); ajax.timeout(callback, script); },
createXHR: function(){ //建立對象 xhr = ajax.getXHR(); xhr.open(opts.type, opts.url, opts.async); //設置請求頭 if (opts.type === "POST" && !opts.contentType) { //如果post提交,則設置content-Type 爲application/x-www-four-urlencoded xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); } else if (opts.contentType) { xhr.setRequestHeader("Content-Type", opts.contentType); } //添加監聽 xhr.onreadystatechange = function() { if (xhr.readyState === 4) { if (opts.timeout !== undefined) { //因爲執行abort()方法後,有可能觸發onreadystatechange事件,因此設置一個ajax.options.timeoutBool標識,來忽略停止觸發的事件。 if (ajax.options.timeoutBool) { return; } clearTimeout(ajax.options.timeoutFlag); } if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) { var responseText = xhr.responseText; try { xhr.responseText && (responseText = JSON.parse(responseText)); opts.success(responseText); } catch (e) { console.error('ajax error for json parse responseText'); //opts.error(xhr); } } else { opts.error(xhr); } } }; //發送請求 xhr.send(opts.type === "GET" ? null : opts.data); ajax.timeout(); //請求超時 }
getXHR: function(){ if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else { //遍歷IE中不一樣版本的ActiveX對象 var versions = ["Microsoft", "msxm3", "msxml2", "msxml1"]; for (var i = 0; i < versions.length; i++) { try { var version = versions[i] + ".XMLHTTP"; return new ActiveXObject(version); } catch (e) { console.log('error ajax',e) } } } }
前面我定義了一個全局的屬性timeoutFlag,這裏經過settimeout延時函數給它賦值。若是是jsonp,則移除原來追加的script標籤,不然經過全局的xhr條用abort()方法終止正在發送的請求!javascript
timeout: function(callback, script){ if (opts.timeout !== undefined) { ajax.options.timeoutFlag = setTimeout(function() { if (opts.dataType === "jsonp") { delete window[callback]; document.body.removeChild(script); } else { ajax.options.timeoutBool = true; xhr && xhr.abort(); } }, opts.timeout); } },
var defaultOpts = { url: '', //ajax 請求地址 type : 'GET', //請求的方法,默認爲GET data : null, //請求的數據 contentType : '',//請求頭 dataType : 'json', //請求的類型,默認爲json async : true, //是否異步,默認爲true timeout: 5000, //超時時間,默認5秒鐘 before : function() { console.log('before') }, //發送以前執行的函數 error: function() { console.log('error') }, //錯誤執行的函數 success: function() { console.log('success') } //請求成功的回調函數 } for (i in defaultOpts) { if (opts[i] === undefined) { opts[i] = defaultOpts[i]; } } var xhr = null; options: { timeoutFlag: null, //超時標識 timeoutBool: false //是否請求超時 },
init: function(){ opts.before(); ajax.getData(); opts.dataType === "jsonp" ? ajax.createJsonp() : ajax.createXHR(); }, ajax.init();
// AMD && CMD if(typeof define === 'function'){ define(function(){ return li; }); // CommonJS }else if(typeof module !== "undefined" && module !== null){ module.exports = li; // window }else{ window.li = li; }