JQuery九种与后台交互数据的方法(ajax)

本文详细介绍了使用JQuery与后台交互数据的九种方法,包括getJson、get、post、ajax、load以及使用AjaxForm插件进行表单提交等。示例代码展示了如何从后台获取并处理Json格式的数据。
摘要由CSDN通过智能技术生成

首先创建一个一般处理程序“ResponseJson.ashx”,手动拼接字符串返回Json格式:

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            List<cityInfo> cityInfos = new List<cityInfo>()
            {
              new cityInfo(){cityID=1,cityName="北京"},
              new cityInfo(){cityID=2,cityName="上海"},
              new cityInfo(){cityID=3,cityName="南京"},
              new cityInfo(){cityID=4,cityName="深圳"},
              new cityInfo(){cityID=5,cityName="广州"}
            };
           
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            foreach (cityInfo cityInfo in cityInfos)
            {
                    sb.Append("{");
                    sb.AppendFormat("\"cityID\":{0},\"cityName\":\"{1}\"", cityInfo.cityID, cityInfo.cityName);
                    sb.Append("}");
                    sb.Append(",");
            }
            string str = sb.ToString().TrimEnd(',');
            str += "]";

            context.Response.Write(str);

       // 或者将对象序列化成Json格式 和拼接相比这种方式较为死板

            //System.Web.Script.Serialization.JavaScriptSerializer JSSerializer = new JavaScriptSerializer();
            //string Json = JSSerializer.Serialize(cityInfos);
            //context.Response.Write(Json);


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

    public class cityInfo
    {
        public string cityName
        {
            get;
            set;
        }

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值