ajax调用ashx页面内的方法

//$.ajax的post方式
function CommentAll() {
    $.ajax({
        url: "/ashx/myzhuye/Detail.ashx",
        type: "post",
        contentType: "application/x-www-form-urlencoded; ", //默认,表单提交格式
        dataType: "json",
        data: { methodName: "GetAllComment", str1: "atr1", "str2": "str2" },
        success: function (data) {
            $.each(data,
               function (i, v) {
                    alert(v);
                });
        }
    });
}


 private  void GetAllComment(HttpContext context)
        {
            //Params可以取得get与post方式传递过来的值。
            string methodName = context.Request.Params["methodName"];

            //Form只能取得post方式传递过来的值。
            string str1 = context.Request.Form["str1"];

            //取得httpRequest传来的值,包括get与post方式
            string str2 = context.Request["str2"];

            List<string> comments = new List<string>();
            comments.Add(methodName);
            comments.Add(str1);
            comments.Add(str2);

            //ajax接受的是json类型,需要把返回的数据转为json格式,要是不转的话只能返回text格式,前台datatype要改为text.
            string commentsJson = new JavaScriptSerializer().Serialize(comments);

            context.Response.Write(commentsJson);
        }



//$.ajax的get方式  (后台还是GetAllComment()方法)
function CommentAll() {
    $.ajax({
        url: "/ashx/myzhuye/Detail.ashx",
        type: "get",
        contentType: "application/json; ", //get方式下jsonn格式也可以
        dataType: "json",
        data: { methodName: "GetAllComment", str1: "atr1", "str2": "str2" },
        success: function (data) {
            $.each(data,
                function (i, v) {
                    alert(v);
                });
        }
    });
}

//$.get请求方式  
//该请求只能返回text数据类型,不能返回数组、json等
//$.get(url,[data],[callback])
function CommentAll() {
    $.get("/ashx/myzhuye/Detail.ashx",
        { methodName: "GetAllComment", str1: "atr1", "str2": "str2" },
        function(da) {
            alert(da);
        });
}
//后台还是GetAllComment()方法,需要把返回的类型改为字符串



//$.getJSON方式,跟get唯一的差别是返回的是json格式
//$.getJSON(url,[data],[callback])
function CommentAll() {
    $.getJSON("/ashx/myzhuye/Detail.ashx",
        { methodName: "GetAllComment", str1: "atr1", "str2": "str2" },   
        function (da) { 
            $.each(da,function(i, v) { 
                alert(v);    
            }) 
        });
}

//$.post(url,[data],[callback],[type]),
//$.post请求方式,跟$get差不多,返回的也是字符串,只是多了个type可设置属性,可设为html,xml,json等类型
function CommentAll() {
    $.post("/ashx/myzhuye/Detail.ashx",
        { methodName: "GetAllComment", str1: "atr1", "str2": "str2" },
        function (da) {
            $.each(da, function (i, v) {
                alert(v);
            });
        }, "json");
}



扩展:https://www.cnblogs.com/gxbk629/p/5795568.html
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值