Ajax的四种请求方式

==============================================================html页面================================
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="jquery-1.10.2.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnGetJson").click(
                function () {
                    $.getJSON("ResponseJson.ashx", "a=3&b=4", function (data) {
                        alert(data[1].CityName);
                    });
                });
            $("#btnJQGet").click(function () {
                $.get("ResponseJson.ashx", "aa=22", function (data) {
                    alert(data);
                });
            });
            $("#btnJQPost").click(function () {
                $.post("ResponseJson.ashx", "bb=22", function (data) {
                    alert(data[0].CityName);
                }, "json")
            });
            $("#btnAjax").click(
                function () {
                    $.ajax({
                        url: "ResponseJson.ashx",
                        data: "a=3",
                        type: "Post",
                        success: function (data) {
                            //alert(data);
                            $("#divDemo").text(data);
                        },
                        error: function () {
                            alert("错误!");
                        }
                    });
                }
                );
            $("#btnLoad").click(function () {
                $("#divDemo").load("ResponseJson.ashx", { id: 333 }, function (data) {
                    alert(data);
                });  
            });
        });
    </script>
</head>
<body>
    <input id="btnGetJson" type="button" value="JQ GetJson" />
    <input type="button" value="JQ Get" id="btnJQGet" />
    <input type="button" value="JQ Post" id="btnJQPost" />
    <input type="button" value="JQ ajax" id="btnAjax" />
    <input type="button" value="JQ load" id="btnLoad" />
    <div id="divDemo">


    </div>
</body>
</html>
===========================================================================ashx================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;


namespace _20141224.Ajax
{
    /// <summary>
    /// Summary description for ResponseJson
    /// </summary>
    public class ResponseJson : IHttpHandler
    {


        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            System.Collections.Generic.List<CityInfo> cities = 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 = "许昌" },
                new CityInfo() { CityId = 6, CityName = "开封" },
                new CityInfo() { CityId = 7, CityName = "巩义" }
};
            //StringBuilder sb = new StringBuilder();
            //sb.Append("[");
            //foreach (var CityInfo in cities)
            //{
            //    sb.Append("{");
            //    sb.AppendFormat("\"CityId\":\"{0}\",\"CityName\":\"{1}\"", CityInfo.CityId,CityInfo.CityName);
            //    sb.Append("},");
            //}
            //string str = sb.ToString().TrimEnd(',');
            //str += "]";




            System.Web.Script.Serialization.JavaScriptSerializer javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string json = javaScriptSerializer.Serialize(cities);
            context.Response.Write(json);
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public class CityInfo
        {
            public string CityName { get; set; }
            public int CityId { get; set; }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值