jQuery Ajax 仿AjaxPro.Utility.RegisterTypeForAjax辅助方法

简介:

        我们都知道在AjaxPro的方法AjaxPro.Utility.RegisterTypeForAjax(typeof(所在类的类名));会将标记有[Ajax.AjaxMethod]方法注册在客户端。在某项目中,设计模板字段引擎,采用html+jquery实现,这里的数据就难免需要ajax获取,但是团队对于js掌握不一,所以我写了下面辅助类,可以像ajaxpro一样简化ajax的开发。

代码-jQueryInvokeMethodAttribute (此处只做标示方法处理,所以为空):

[AttributeUsage(AttributeTargets.Method, AllowMultiple= false,Inherited= false)] 
     public  class jQueryInvokeMethodAttribute : Attribute 
    { 
    }

代码-jQueryAjaxUtility(分注册脚本和调用ajax事件):

ExpandedBlockStart.gif
复制代码
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Green.Utility 

     public  class jQueryAjaxUtility 
    { 
         public  static  string AjaxInvokeParam =  " AjaxInvoke "
         public  static  string AjaxInvokeValue =  " 1 "
         public  static  string ResponseCharset =  " UTF-8 "

         protected  static System.Web.UI.Page Page 
        { 
             get 
            { 
                 return System.Web.HttpContext.Current.Handler  as System.Web.UI.Page; 
            } 
        } 

         public  static  void RegisterClientAjaxScript(Type type) 
        { 
             if (Page !=  null
            { 
                 if (System.Web.HttpContext.Current.Request[AjaxInvokeParam] == AjaxInvokeValue) 
                { 
                    RegisterAjaxInvokeEvent(type); 
                } 
                 else 
                { 
                    RegisterAjaxInvokeScript(type); 
                } 
            } 
        } 

         protected  static  void RegisterAjaxInvokeScript(Type type) 
        { 

            Page.ClientScript.RegisterClientScriptBlock(type.GetType(), type.GetType().FullName +  " _ " +  typeof(jQueryAjaxUtility).FullName +  " _AjaxInvokeDefaultOption "" window.defaultAjaxOption={type:'GET',cache:false, dataType:'text'}; "true); 

             if (!jQueryUtilityCache.Current.Exists(type)) 
            { 
                var methodinfo = type.GetMethods(System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public).Where(t => 
                { 
                    var attrs = t.GetCustomAttributes( typeof(jQueryInvokeMethodAttribute),  false); 
                     if (attrs !=  null && attrs.Length >  0
                         return  true
                     return  false
                }).ToList(); 

                 if (methodinfo !=  null && methodinfo.Count >  0
                { 
                    System.Text.StringBuilder sb =  new StringBuilder(); 
                    sb.AppendFormat( "  window.{0}=function(){{}};  ", type.Name); 
                    methodinfo.ForEach(t => 
                    { 
                        var parameters = t.GetParameters().Select(p => p.Name).ToArray(); 
                        sb.AppendFormat( "  {2}.{0} = function ({1} ajaxOption) {{ ", t.Name, parameters.Count() >  0 ?  string.Join( " , ", parameters) +  " , " :  "", type.Name); 
                        sb.Append( " if(ajaxOption==null||typeof ajaxOption=='undefined'){ajaxOption={};}; "); 
                        var url = Page.Request.RawUrl.IndexOf( " ? ") == - 1 ? Page.Request.RawUrl : Page.Request.RawUrl.Substring( 0, Page.Request.RawUrl.IndexOf( " ? ") ); 
                        sb.AppendFormat( " ajaxOption.url = '{0}'; ", url); 
                        var data =  " '' "
                         if (parameters.Count() >  0
                        { 
                            data = ( string.Join( "   ", parameters.Select(p =>  string.Format( " '&{0}=' + {0}+ ", p)).ToArray())); 
                           data= data.TrimEnd( ' + '); 
                        } 
                        sb.AppendFormat( " ajaxOption.data = 'method={1}&rn={4}&{2}={3}'+{0}; ", data, t.Name, AjaxInvokeParam, AjaxInvokeValue,Guid.NewGuid().ToString()); 

                        sb.Append( " ajaxOption= jQuery.extend(window.defaultAjaxOption,ajaxOption); "); 
                        sb.Append( " jQuery.ajax(ajaxOption);}; "); 
                    }); 
                    jQueryUtilityCache.Current.AddScript(type, sb.ToString()); 
                } 
            } 
            var script = jQueryUtilityCache.Current.GetScript(type); 
            Page.ClientScript.RegisterClientScriptBlock(type.GetType(), type.GetType().FullName +  " _ " +  typeof(jQueryAjaxUtility).FullName +  " _AjaxInvoke ", script,  true); 
        } 

         protected  string GenertorScript(Type type) 
        { 

             return  string.Empty; 
        } 

         protected  static  void RegisterAjaxInvokeEvent(Type type) 
        { 

            var Request = System.Web.HttpContext.Current.Request; 
            var Response = System.Web.HttpContext.Current.Response; 
            var method = Request[ " method "]; 
             if ( string.IsNullOrEmpty(method)) 
                 return
            Response.Clear(); 
            var methodinfo = type.GetMethod(method, System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public); 
             if (methodinfo !=  null
            { 
                Response.Charset = ResponseCharset; 
                Response.ContentType =  string.Join( " , ", Request.AcceptTypes); 
                var param = methodinfo.GetParameters(); 
                 object[] objs =  new  object[param.Length]; 
                var i =  0
                param.ToList().ForEach(t => 
                { 
                    objs[i++] = Convert.ChangeType(Request[t.Name], t.ParameterType); 
                }); 
                var obj = methodinfo.Invoke( null, objs); 
                 if (obj !=  null
                { 
                     // 序列化 
                     if (!obj.GetType().IsValueType && obj.GetType() !=  typeof( string)) 
                    { 
                         if (Request.AcceptTypes.Contains( " text/xml ")) 
                        { 
                            Response.Write(Green.Utility.SerializerUtility.XmlSerializer(obj)); 
                        } 
                         else  if (Request.AcceptTypes.Contains( " application/json ")) 
                        { 
                            Response.ContentType =  " application/json, text/javascript, */* "
                            Response.Write(Green.Utility.SerializerUtility.JsonSerializer(obj)); 
                        } 
                         else 
                        { 
                            Response.Write(obj); 
                        } 

                    } 
                     else 
                    { 
                        Response.Write(obj); 
                    } 
                } 

                Response.Flush(); 
                Response.Close(); 
                Response.End(); 
            } 
        } 

    } 
}
复制代码

为了考虑反射的性能,加入了类级注册脚本方法缓存处理jQueryUtilityCache,具体见demo。

测试:

html:

< form  id ="form1"  runat ="server" > 
     < div > 
         < input  id ="Button1"  type ="button"  value ="button"   /> 
     </ div > 
     </ form >

后台方法注册Page_Load

Green.Utility.jQueryAjaxUtility.RegisterClientAjaxScript( typeof(_Default));

1:

前台:

复制代码
_Default.Test("ajax", 
                             { 
                                 success:  function(e) { 
                                     alert(e); 
                                 }, 
                                 dataType: "text" 
                             });
复制代码

后台:

[Green.Utility.jQueryInvokeMethod()] 
    public  static  string Test( string str) 
   { 
        return  " hello: " + str; 
   } 

效果:

image

2:

前台ajax:

复制代码
_Default.TestArrayJson(1, 2, 3, 
              { 
                  success:  function(e) {                                  
                      $.each(e,  function(i, n) { alert(n); }); 
                  }, 
                  dataType: "json" 
              }) 
复制代码

后台:

复制代码
[Green.Utility.jQueryInvokeMethod()] 
public  static  int[] TestArrayJson( int p1,  int p2,  int p3) 

     return  new  int[] { p1, p2, p3 }; 
}
 
复制代码

效果:

image 

3:

前台ajax:

复制代码
_Default.TestArrayxml("key", "value", 
            { 
                success:  function(e) { 
                    alert(e.key); 
                    alert(e.Value); 
                }, 
                dataType: "json" 
            })
复制代码

后台:

[Green.Utility.jQueryInvokeMethod()] 
     public  static Test TestArrayxml( string key, string value) 
    { 
         return  new Test() { key=key,Value=value}; 
    }

效果:

image

最后看看FireBug中ajax http头信息:

image

 

其他资料jQuery目录:  我jQuery系列之目录汇总

附录:代码下载


作者:破  狼 
出处:http://www.cnblogs.com/whitewolf/ 
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该文章也同时发布在我的独立博客中-个人独立博客博客园--破狼51CTO--破狼。http://www.cnblogs.com/whitewolf/archive/2011/09/26/2192253.html


相关文章
|
4天前
|
JavaScript CDN
jQuery方法小记
jQuery方法小记
15 0
|
17天前
|
JavaScript 前端开发 容器
AJAX载入外部JS文件到页面并让其执行的方法(附源码)
AJAX载入外部JS文件到页面并让其执行的方法(附源码)
18 0
N..
|
1月前
|
XML JSON 前端开发
jQuery实现Ajax
jQuery实现Ajax
N..
18 1
|
3月前
|
JavaScript
jQuery追加节点方法 和height方法与width方法
jQuery追加节点方法 和height方法与width方法
|
1月前
|
前端开发
AJAX发送请求方法封装和请求函数底层刨析以及axios二次封装
AJAX发送请求方法封装和请求函数底层刨析以及axios二次封装
|
1月前
|
移动开发 前端开发 安全
Ajax跨域的所有方法(最详细带使用教程!!!)
Ajax跨域的所有方法(最详细带使用教程!!!)
|
2月前
|
JavaScript 前端开发 Java
jquery ajax+spring mvc上传文件
jquery ajax+spring mvc上传文件
|
2月前
|
数据采集 Web App开发 前端开发
Python爬虫之Ajax分析方法与结果提取#6
Ajax分析方法、Ajax结果提取【2月更文挑战第20天】
37 0
Python爬虫之Ajax分析方法与结果提取#6
|
2月前
|
XML 前端开发 JavaScript
AJAX get() 和 post() 方法
AJAX(Asynchronous JavaScript and XML)是一种用于创建快速和动态网页的技术,它允许使用 JavaScript 和 XMLHttpRequest 对象在不重新加载整个页面的情况下向服务器发送请求和接收响应。jQuery 提供了几个用于 AJAX 操作的方法,包括 .ajax()、.get() 和 .post()
15 1
|
3月前
|
前端开发 JavaScript