The simple Ajax function handles the of sample code

  • 2020-03-29 23:46:48
  • OfStack

The following is the packaging of a handler function, is also very convenient to use:

The name of the file is jQuery. Ajaxrequester.js.

Usage:


$('._ajax').click(function(){
 _ajax.request(this);
  return false;  
});
var _ajax = {
      request: function(o){
             var tform = $(o).parents('form');
             _ar2(tform,'','_ajax._fb_request');
      },
      _fb._request: function(d){
         d = eval('(' +unescape(d)+ ')');                                                                                                //Submit a status prompt.
  if(d.notification){
   if(d.type == 'ok'){
    alert(' Submitted successfully ');    
   }else if(d.type == 'error'){
    alert(' You submit ');                                                                                                       
                 }else{
    alert(' error ');
   }
  }
 }
}

The ajax file code:

//Improved version _ar
function _ar2(s, target, feedback, param, methods){
 var url, method;
 var params = {};
 if( (s instanceof jQuery) && s.attr("tagName")=='VFORM') {
  url  = s.attr('action');
  method = s.attr('method');
  params = param || $('input, textarea, select',s).serialize();
 }else if( (s instanceof jQuery) && s.attr("tagName")=='FORM') {
  url  = s.attr('action');
  method = s.attr('method');
  params = param || s.serialize();
 }else if(typeof(s)=='string'){
  url  = s; // http://...
 }
 url = url || '';
 method = method || methods || 'GET';
 params = param ? param : params;
 jQuery.ajax({
  type: method,url:url,data:params,
  beforeSend: function(){
   //JQuery. JGrowl (' please wait while the data is read.. '{  Header: 'prompt ', theme: 'blue'});
  },
  success: function(data) {
   //data = jQuery.trim(data).replace(/[ntr]/gi,'');
   if(feedback){
    eval(feedback+'(''+escape(data)+'')');
    return;
   }else{
    if(data){
     if(target){
      jQuery(target).html(data);
      jQuery.jGrowl(' Page request completed .', {  header: ' prompt ', theme: 'green', 'life': 100});
      return;
     }
    }else{
     lg('no feedback');
     jQuery.jGrowl(' The server returned no information .', {  header: ' prompt ', theme: 'blue'});
     return;
    }
   }
   jQuery.jGrowl(data, {  header: ' prompt ', theme: 'blue'});
  },
  error: function (e){
   var s = e.status;
   switch(s) {
    case 404:
     jQuery.jGrowl(' The requested page could not be found .  Please contact your system administrator .', {  header: ' prompt ', theme: 'blue'});
    break;
    case 500:
     jQuery.jGrowl(' Server error [500].  Please contact your system administrator .', {  header: ' prompt ', theme: 'blue'});
    break;
    default:
     //log('unknow error');
     lg(s);
     jQuery.jGrowl(' An unknown error .  Please contact your system administrator .', {  header: ' prompt ', theme: 'blue'});
   }
  }
 });
}

Note: jQuery. JGrowl is a jQuery popover prompt plug-in, prompt complete automatically disappear, it is very convenient to use, Google to know. In addition, as for the use method, the returned data is a good format agreed by our company, which is convenient to use.


Related articles: