zoukankan      html  css  js  c++  java
  • jquery实现ajax,返回json数据

    jquery实现ajax可以调用几种方法

    我经常用的是$get(url,data,callback,type)方法

    其中url是异步请求的页面(可以是.ashx文件),data是参数,callback是回调函数,而type是返回数据的类型.type有xml,html,json,text等.

    首先,页面引用jquery.js

    在页面写ajax处理的js函数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    function initMeeting() {
                $.get("/Common/MeetingRoom.ashx", {meetid:<%=meetId %>},function sellerList(data){
                    $("#divSellerList").html(data);
                },"json");
                setTimeout("initMeeting()",20000);
            }
            function initMeeting() {
                $.get("/Common/MeetingRoom.ashx", {meetid:<%=meetId %>},function sellerList(data){
                    var obj = eval( "(" + data + ")" );//转换后的JSON对象
                    $("#divSellerList").html(obj.CellerList);
                },"html");
                setTimeout("initMeeting()",20000);
            }

    我用的返回类型是json,这样可以返回类似类的数据类型.比如{"Name":"Sunny D.D", "Age":25}

    但是在使用返回值data时,首先要转换json,通过

    1
    var obj = eval( "(" + data + ")" );//转换后的JSON对象

    就能获得json对象.

    json对象是在MeetingRoom.ashx文件里处理生成的

    部分代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    public class MeetingRoom : IHttpHandler
    {
     
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            try
            {
                int meetid = XYConvert.GetInt32(context.Request.QueryString["meetid"]);
     
                string str = "";
     
                MeetingJson meetingJson = new MeetingJson();
     
                if (meetid != 0)
                {
                    meetingJson.CellerList=returnCellerList(meetid);
                     
                }
                str = JsonConvert.SerializeObject(meetingJson);
                context.Response.Write(str);
            }
            catch (Exception ex) {
                context.Response.Write(ex.Message);
            }
        }
        public class MeetingJson {
            string cellerList;
     
            public string CellerList
            {
                get { return cellerList; }
                set { cellerList = value; }
            }
        }
        protected string returnCellerList(int meetid)
       {
              //省略..
       }
    }

    返回json数据格式最重要的是使用了JsonConvert.SerializeObject()方法.它可以将需要传递到客户端的数据打包,并序列化为字符串

    而类JsonConvert在第三方dll文件中(Newtonsoft.Json.dll),引入dll就可以使用了.

    注意:在.ashx页面中,想要使用Session的话,直接写context.Session["user"]是不行的,必须指定当前上下文可以使用Session,可已实现IRequiresSessionState接口,访问Session

    1
    2
    public class MeetingRoom : IHttpHandler, IRequiresSessionState
    {

    在中小项目中,使用这种方式实现ajax,如果是大项目里,应该有封装更好的ajax框架

  • 相关阅读:
    Linux命令全训练
    解决maven中静态资源只能放到properties中的问题
    Mybatis出现错误org.apache.ibatis.executor.ExecutorException: No constructor found in
    Fence Repair
    Saruman's Army
    Best Cow Line
    区间调度问题
    硬币问题
    迷宫最短路径
    Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) A. Oath of the Night's Watch
  • 原文地址:https://www.cnblogs.com/Gbeniot/p/3884230.html
Copyright © 2011-2022 走看看