Jquery ajax使用json形式通信

前台JS
$.ajax({
                             type: ‘post‘,
                             url: ‘HandlerLogin.ashx‘,
                             data: { name: $( ‘#txtLoginName‘).val(), pwd: $(‘#txtPwd‘ ).val() },
                             dataType: ‘json‘,
                             success: function (result) {
                                 loginAndRegDialog.dialog( ‘close‘);
                                 $.messager.show({
                                     title: ‘提示‘,
                                     msg: ‘登陆成功‘
                                 });
                             },
                             error: function (XmlHttpRequest, textStatus, errorThrown) {
                                 console.info(XmlHttpRequest);
                                 console.info(textStatus);
                                 console.info(errorThrown);
                             }
                         });
 
 
后台处理:
public class HandlerLogin : IHttpHandler
    {
 
        public void ProcessRequest(HttpContext context)
        {
            if (!string .IsNullOrEmpty(context.Request["name"]))
            {
                string name = context.Request["name" ];
                context.Response.Write(LoginValidate(name));
            }
            else {
                return;
            }
        }
 
        /// <summary>
        /// 根据登陆名判断时候存在该用户
        /// </summary>
        /// <param name="loginName"></param>
        /// <returns> 返回json字符串 </returns>
        private string LoginValidate(string loginName) {
            string json="" ;
            string constr = ConfigurationManager .ConnectionStrings["miceSysbase"].ToString();
            using (SqlConnection conn=new SqlConnection(constr))
            {
                string sql = "select * from Aut_User where LoginInfo=‘" + loginName + "‘";
                SqlDataAdapter sa = new SqlDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                sa.Fill(dt);
                if (dt.Rows.Count>0)
                {
                    DataRow dr = dt.Rows[0];
                    json = ViewToJsonData(dr);
                }
                else {
                    json = "{‘false‘:[{‘UserID‘:‘‘,‘LoginInfo‘:‘‘,‘UserName‘:‘‘,‘CreateTime‘:‘‘}]}" ;
                }
            }
            return json;
        }
 
        /// <summary>
        /// 根据查询结果序列化数据
        /// </summary>
        private string ViewToJsonData(DataRow dr)
        {
            string json = "{\"UserID\":\"" + dr["UserID"].ToString() + "\",\"LoginInfo\":\"" + dr["LoginInfo" ].ToString() + "\",\"UserName\":\"" + dr["UserName" ].ToString() + "\",\"CreateDate\":\"" + dr["CreateDate" ].ToString() + "\"}";
            return json;
        }
    }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。