c# Webmethod public static void消息返回Jquery

本文关键字:消息 返回 Jquery void static Webmethod public | 更新日期: 2023-09-27 17:54:54

嗨,我需要返回一个消息回Jquery。我的webmethod和SQL脚本做他们的事情,我不需要担心异常错误的时刻。

*重要的消息是计数> 0,因为这将告诉用户,电子邮件已经注册,通过Jquery警报。

[WebMethod]
[ScriptMethod]
public static void SaveUser(Saved user)
{
    string constr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        try
        {
            using (SqlCommand cmd = new SqlCommand("select count(*)from TestTable2 where Email=@Email"))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@Email", user.Email);
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                int count = Convert.ToInt32(cmd.ExecuteScalar());
                con.Close();
                if (count > 0)
                {
                    // ...message email taken!
                }
                else
                {
                    // now do Insert!
                }
            }
        }
        catch (Exception E)
        {
            StreamWriter sw = new StreamWriter(@"C:'inetpub'wwwroot'jQuery_AJAX_Database'error.txt", true);
            sw.WriteLine(E.Message + user.Email);
            sw.Close();
            sw.Dispose();
            throw new Exception(E.Message);
        }
    }
}
$.ajax({
            type: "POST",
            url: "my_dev3.aspx/SaveUser",
            data: '{user: ' + JSON.stringify(user) + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function () {
                alert('OK!');
            },
            error: function () {
                alert('Error!')
            }
        });
        return false;

c# Webmethod public static void消息返回Jquery

试试这个:

[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public static string SaveUser(Saved user)
{
    string constr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        try
        {
            using (SqlCommand cmd = new SqlCommand("select count(*)from TestTable2 where Email=@Email"))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@Email", user.Email);
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                int count = Convert.ToInt32(cmd.ExecuteScalar());
                con.Close();
                if (count > 0)
                {
                    return new JavaScriptSerializer().Serialize(new { Message = "Email Taken"});
                }
                else
                {
                    return new JavaScriptSerializer().Serialize(new { Message = "Email Inserted"});
                }
            }
        }
        catch (Exception E)
        {
            StreamWriter sw = new StreamWriter(@"C:'inetpub'wwwroot'jQuery_AJAX_Database'error.txt", true);
            sw.WriteLine(E.Message + user.Email);
            sw.Close();
            sw.Dispose();
            throw new Exception(E.Message);
            return new JavaScriptSerializer().Serialize(new { Message = "Error Occured"});
        }
    }
}

$.ajax({
            type: "POST",
            url: "my_dev3.aspx/SaveUser",
            data: '{user: ' + JSON.stringify(user) + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                    alert(data.Message);
            },
            error: function (data) {
                alert(data.Message)
            }
        return false;
});