Ajax架構(UTF-8編碼),POST傳輸

來源:互聯網
上載者:User

/*  
 * The source code for this program is not published or otherwise divested of
 * its trade secrets.
 *
 */

/** Ajax 請求的封裝類
 * Example:
 *  //執行個體化一個請求對象
 *  var ajaxObj = new AjaxProxyII();
 *  //設定ajax請求的url
 *  ajaxObj.setRequestUrl("localhost/projectName/path/someAction.action")
 *  //設定回呼函數 回呼函數 要有兩個參function someFunc(data , textStatus);
 *  ajaxObj.setOnSuccess(someFunc);
 *  //設定要傳遞的參數 如果參數是對象形式
 *  ajaxObj.addParaObj(paraObject);
 *  //如果參數為索引值對形勢
 *  ajaxObj.addParameter("key" , value);
 *  //設定同步非同步 預設為非同步
 *  ajaxObj.setAsync(false);
 *  //發送請求
 *  ajaxObj.sendRequest();
 */
var AjaxProxyII = function(){
 this.parameter = new Object();
 this.pojoPackagePath = "com.taiyang.search.bean";
 this._props = new Object();
 this._argArray = new Array();
 this._pojoName = '';
 this._paraStr = '';
 this._props['asyn'] = true;
 this.setPojoName = function(pojoName){
  this._pojoName = this.pojoPackagePath + pojoName ;
 }
 
 this.setRequestUrl = function(url){
  this._props['url'] = url;
 }
 
 this.setOnSuccess = function(func){
  this._props['onSuccess'] = func;
 }
 
 this.addParaObj = function(obj){
  this._argArray.push(obj);
 }
 
 this.sendRequest = function(){
  var callfunc=this._props['onSuccess'];
  this.setupParaStr();
  $.ajax({
   
   type : 'POST',
   url : this._props['url'],
   success :function(data){
    if(data.indexOf('errormessage')>-1){
     var tmpObj = eval( '(' + data + ')' );
     if(tmpObj['errorcode']=="0"){
      var windowsArr=new Array();
      getWindowTop(window,windowsArr,tmpObj['errormessage']);
      return;
     }else if(tmpObj['errorcode']=="1"){
      window.location.href=homeUrl+"/error.jsp?errormessage="+encodeURI(encodeURI(tmpObj['errormessage']));
      return;
     }
    }
    callfunc(data);
   },
   data :this._paraStr,
   async : this._props['asyn'],
   error : function(data , textStatus){
    //alert("錯誤" + data);
   }
  });
 }
 
 this.setParaStr = function(str){
  this._paraStr = str;
 }
 
 this.addParameter = function(key , value){
  this.parameter['' + key] = value;
 }
 this.setAsync = function(flag){
  this._props['asyn'] = flag;
 }
 this.setupParaStr = function(){
  
  if(this._pojoName != 'undefined' && this._pojoName != ''){
   this._paraStr += '&pojoName=' + this._pojoName  + '&jsonData=';
   this._paraStr += JSON.stringify(this._argArray);
   
  }
  for(var i = 0 ; i<this._argArray.length; i++){
   this._argArray[i] = null;
  }
  for(var p in this.parameter){
   this._paraStr += p + '=' + this.parameter[p] + '&';
  }
//  this._paraStr = "亂碼"
  //this._paraStr = encodeURI(this._paraStr);
  
 }  
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.