C#Webmethod将public static void消息返回给Jquery

时间:2015-06-09 11:22:02

标签: c# jquery

嗨,我需要将一个消息归还给Jquery。我的webmethod和SQL脚本做了他们的事情,我不需要担心异常错误。

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

[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;

1 个答案:

答案 0 :(得分:0)

试试这个:

[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

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