Home  >  Article  >  Web Front-end  >  Simple Ajax function processing (sample code)_jquery

Simple Ajax function processing (sample code)_jquery

WBOY
WBOYOriginal
2016-05-16 17:15:011192browse

The following is an encapsulated processing function, which is very convenient to use:

The file name is: jQuery.ajaxRequest.js .

Usage:

Copy code The code is as follows:

$('._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) ')');                                                                                                                                         
if(d.notification){
if(d.type == 'ok'){
alert('Submission successful');
}else if(d.type == 'error '){
alert('Submission failed');                                                                                                                                                                                                                                        🎜>
The ajax file code:




Copy the code


The code is as follows:

// 改进版 _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('数据读取中 请稍候..', {  header: '提示', 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('页面请求完毕.', {  header: '提示', theme: 'green', 'life': 100});
      return;
     }
    }else{
     lg('no feedback');
     jQuery.jGrowl('服务器无返回信息.', {  header: '提示', theme: 'blue'});
     return;
    }
   }
   jQuery.jGrowl(data, {  header: '提示', theme: 'blue'});
  },
  error: function (e){
   var s = e.status;
   switch(s) {
    case 404:
     jQuery.jGrowl('请求的页面无法找到. 请联系系统管理员.', {  header: '提示', theme: 'blue'});
    break;
    case 500:
     jQuery.jGrowl('服务器错误[500]. 请联系系统管理员.', {  header: '提示', theme: 'blue'});
    break;
    default:
     //log('unknow error');
     lg(s);
     jQuery.jGrowl('未知错误. 请联系系统管理员.', {  header: '提示', theme: 'blue'});
   }
  }
 });
}


注意:jQuery.jGrowl是jquery的一个弹窗提示的插件,提示完成自动消失,用起来很方便,google一下就知道了。另外至于使用方法,返回数据是我们公司约定好的一个格式,方便使用而已。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn