How to create generic request processing method using javascript or ajax

SendRequest.js File (Generic Request Processing file)

 
function sendRequest($ajaxUrl,$arguments,$timeStamp,$ajaxResponseLayer){
 
 var $currentTime = new Date();
 var $timeStamp = $currentTime.getHours() + $currentTime.getMinutes()
 + $currentTime.getSeconds() + $currentTime.getMilliseconds()
 + $currentTime.getDay() + $currentTime.getMonth()
 + $currentTime.getFullYear() + Math.random();

 $.ajax({
 url : $ajaxUrl + "?" + $arguments + "&stamp=" + $timeStamp,
 cache : false,
 beforeSend : function() {
 /* call method before send request to server  */
 showProgressBar();
 },
 complete : function($response, $status) {
 /* hide progress bar once we get response */
 hideProgressBar();
 if ($status != "error" && $status != "timeout") {
 /* for set response in div */
 if($ajaxResponseLayer !="")  
 { 
  if($response.responseText.search("showMessage")>-1){
  //alert('in if ::');
  $("#errorDiv").html($response.responseText.trim());
  processAfterResponse($ajaxUrl,$arguments,$timeStamp,$ajaxResponseLayer,$response.responseText);
  return false;
  }
  $("#"+$ajaxResponseLayer).html($response.responseText);
  processAfterResponse($ajaxUrl,$arguments,$timeStamp,$ajaxResponseLayer,$response.responseText);
 }
 
 }
 },
 error : function($obj) {
 /* call when error occurs */
 hideProgressBar();
 alert("Something went wrong while processing your request."+$obj.responseText);
 }
 });  
}

function processAfterResponse($ajaxUrl,$arguments,$timeStamp,$ajaxResponseLayer,$response){
  if($ajaxUrl =='enterURLAfterProcessing.html'){

   }
}

function closeDialogBox(modelId){
  $("#"+modelId).dialog( "close" );
}

function showProgressBar() {
  document.getElementById('ajax_loader').style.display = 'block';
}

function hideProgressBar() {
  document.getElementById('ajax_loader').style.display = 'none';
}

function IsEmail(email) {
  var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return regex.test(email);
}

function onlyIntegerNumber(evt){
  var e = evt; // for trans-browser compatibility
  var charCode = e.which || e.keyCode;
  if(charCode == 9){
    return true;
  }
  if((charCode >=35 && charCode <38 alert="" charcode="" digit="" e.which="=0){" enter="" false="" humanmsg.displaymsg="" if="" lease="" noty="" only="" options="" return=""> 31) && (charCode < 48 || charCode > 57) ){
          alert('Please enter only digit (0-9)');
         return false;
   }else{
    return true;
   }
}

function isNumberKey(evt){
    var e = evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;
    
    if(charCode == 46 || charCode == 9){
     return true;
    }
    if((charCode >=35 && charCode <38 .="" alert="" characters="" charcode="" e.which="=0){" false="" if="" llow="" numeric="" only="" or="" return=""> 31) && (charCode < 48 || charCode > 57) ){
       noty(options);
           return false;
     }else{
    return true;
     }
}
if(typeof String.prototype.trim !== 'function') {
   String.prototype.trim = function() {
     return this.replace(/^\s+|\s+$/g, ''); 
   };
}


Note : We have to take good care while using trim() function of JavaScript. because it has compatibility issue with IE 8 and older version. So we have to put following line of code in JS, where we used trim function, otherwise function will be given an exception so JavaScript will not be processing further.
if(typeof String.prototype.trim !== 'function') {
   String.prototype.trim = function() {
     return this.replace(/^\s+|\s+$/g, ''); 
   };
}

posted under |

2 comments:

Unknown said...

Excellent…Amazing…. I’m satisfied to find so many helpful information here within the put up,for latest php jobs in hyderabad. we want work out extra strategies in this regard, thanks for sharing.

Vipul Chauhan said...

Thank you very much for this pleasing feedback! It means a lot to me!

Post a Comment

Please provide your valuable comment.

Newer Post Older Post Home

Followers

Powered by Blogger.

Populares

Custom Search