• jQuery getJSON


    Html代码:
    <script type="text/javascript" src="/js/jquery-1.4.js"></script>
    <script type="text/javascript">
    function jsonTest1()
    {
         $.ajax({       
                    url:
    "Handler.ashx",
                    data:{
    "type":"ajax"},
                    datatype:
    "json",   
                    type:
    "get",
                    success:
    function(data)
                       {     
                        document.getElementById(
    'div1').innerHTML=data;//因为mime类型是文本 所以返回回来的是json格式的字符串
                       }        
                      
                 });
    }

    function jsonTest2()
    {
           $.getJSON(     
                           
    'Handler.ashx',
                           {
    'type''json','name':'qixuejia' },   //类型格式   
                           function(data)
                           {     
                            
    for(var i=0;i<data.length;i++)
                               {
                                alert(data[i][
    "UserId"])
                               }
                           }        
                          
                     );
    }

    </script>
        
    <form id="form1" runat="server">
        
    <div id="div1">    
        
    </div>
        
    <input  type="button" value="jQuery.ajax()" onclick="jsonTest1()"/>
        
    <input  type="button" value="jQuery.getJSON()" onclick="jsonTest2()"/>
        
    </form>

    Ashx处理程序:如果需要返回json格式的对象,需要把mime类型设置为:"application/json"。

    查看jQuery源文件,可以看出getJSON这样实现的:

    getJSON: function( url, data, callback ) {
      return jQuery.get(url, data, callback, "json");
     },

        public void ProcessRequest(HttpContext context)
        {
            
    if (context.Request.Params["type"].Equals("ajax"))
            {
                context.Response.ContentType 
    = "text/plain";
            }
            
    else
            {
                context.Response.ContentType 
    = "application/json";
            }
            GetInfo(context);
        }

        
    public bool IsReusable
        {
            
    get
            {
                
    return false;
            }
        }

        
    public void GetInfo(HttpContext context)
        {
            System.Collections.Generic.List
    <UserInfo> listUser = UserInfoManage.GetUserInfoBySQL("Select Top 5 * From Userinfo");
            IsoDateTimeConverter timeConverter 
    = new IsoDateTimeConverter();     
            timeConverter.DateTimeFormat 
    = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";  
            
    string ResJsonStr = JsonConvert.SerializeObject(listUser, timeConverter);
            context.Response.Write(ResJsonStr);     
        }
  • 相关阅读:
    Pythonlistsort()
    [转]Python中文乱码问题深入分析
    使用dom4j时SelectNodes()方法报错
    Xpath语法
    wust2012级软件工程新生经验交流会草稿
    Eclipse中部分快捷键
    Dom4j解析XML学习代码
    html5 cocos2d
    mfc mfc100ud.dll丢失问题
    c# 类操作 窗体
  • 原文地址:https://www.cnblogs.com/qixuejia/p/1784375.html
Copyright © 2020-2023  润新知