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;
            }
        });        
    }

 

posted on 2015-01-28 13:17  零点漂移  阅读(8393)  评论(0编辑  收藏  举报