JavaScript 封裝Ajax傳遞的資料

來源:互聯網
上載者:User
JavaScript Array#join在使用Ajax傳輸資料時,少不了對傳遞的字元進行轉碼,我的實現方式是將需要傳遞的資料暫存到一js Bean中,將js Bean放到Array中,產生傳輸參數時對Array中的jsBean進行分解,得到相應屬性資訊並編碼..

var paramBeanList = new Array();<br />Array.prototype.addParamBean=function(paramBeanObj){<br />var index = this.containParamBean(paramBeanObj);<br />if (index != -1) {<br />this[index] = paramBeanObj;<br />} else {<br />this.push(paramBeanObj);<br />}<br />};<br />Array.prototype.clear=function(){<br />if (this.length == 0) {<br />return;<br />}<br />for (var index in this) {<br />this.pop();<br />}<br />};<br />Array.prototype.containParamBean=function(paramBeanObj){<br />var index = -1;<br />if (this.length == 0) {<br />return index;<br />}<br />for (var tempIndex = 0, step = this.length; tempIndex < step; tempIndex++) {<br />if (this[tempIndex].compare(paramBeanObj) == 0) {<br />index = tempIndex;<br />break;<br />}<br />}<br />return index;<br />};<br />var ParamBean = new function(pkCode, opDate, value) {<br />this.pkCode = pkCode;<br />this.opDate = opDate;<br />this.value = value;<br />};<br />ParamBean.prototype={<br /> toString:function() {<br /> return "[pkCode:" + this.pkCode + ",opDate:" + this.opDate +",value:" + this.value + "]";<br />},<br />doVerify:function() {<br />return (this.pkCode ? this.opDate ? this.value ? "true" : "false" : "false" : "false");<br />},<br />compare:function(otherObj) {<br />var result = -1;<br />if (otherObj) {<br />if (this.pkCode == otherObj.pkCode && this.opDate == otherObj.opDate<br />&& this.value == otherObj.value) {<br />result = 0;<br />}<br />}<br />return result;<br />}<br />};<br />var ParamUtils = new Object();<br />ParamUtils.doCreateAjaxStr=function() {<br />var paramStr = "";<br />if (paramBeanList.length == 0) {<br />return paramStr;<br />}<br />var keyParamArray = new Array();<br />var valueParamArray = new Array();<br />for (var index = 0, step = paramBeanList.length; index < step; index++) {<br />var tempObj = paramBeanList[index];<br />keyParamArray.push(tempObj.pkCode + "`" + tempObj.opDate);<br />valueParamArray.push(tempObj.value);<br />}<br />paramStr = "KEY_PARAM=".concat(encodeURIComponent(keyParamArray.join(","))).concat("&").concat("VALUE_PARAM=".concat(encodeURIComponent(valueParamArray.join(","))));<br />return paramStr;<br />};這篇文章我寫了一會,到了csdn上弄了半天提不上去,我用IE6切到進階編輯,內容直接就是空,最後用Firefox瀏覽器竟然又提上來了。。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.