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

 

相关文章:

  • 2021-09-09
  • 2022-12-23
  • 2021-08-21
  • 2021-12-10
  • 2022-12-23
  • 2021-05-17
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-01
  • 2022-12-23
  • 2021-06-13
  • 2021-05-02
  • 2021-11-20
  • 2021-06-06
相关资源
相似解决方案