ashxファイルはajaxと組み合わせて使用されます(jsonデータを返します)

6576 ワード

ashxファイルはjsonデータを返します.
        public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/plain";

            string userName = string.Empty;

            string msg = "{{\"code\":\"{0}\",\"msg\":\"{1}\"}}";

            //  

            if (context.Request["txtUserName"] != null) userName = context.Request["txtUserName"];

            if (string.IsNullOrEmpty(userName))

            {

                context.Response.Write(string.Format(msg, -1, ""));

                return;

            }      

            context.Response.Write(string.Format(msg, 1001, "    "));

        } 

フロントエンドページでjsonデータを処理する方法:
    function Register() {

        //……

        $.ajax({

            type:"post",

            url:"abc.ashx?partner=<%=Request["partner"] %>&s=<%=Request["s"]%>&r=" + Math.random(),

            dataType:"json",

            data:{

                "txtUserName":$('#txtUserName').val()

            },

            async:true,

            success:function (data) {

                var item = eval(data);//   json               

                alert(data.msg);               

                if(item.code == "1001"){ //      ,        

                    $("#success").attr("href","javascript:CloseWin()");

                    $("#succClose").attr("href","javascript:CloseWin()");

                }else{

                    $("#success").attr("href","javascript:$.modal.close()");

                    $("#succClose").attr("href","javascript:$.modal.close()");

                }

                return;

            }

        });        

    }