嗨我需要返回一個messsage回到jquery。我的webmethod和SQL腳本做他們的事情,我現在不需要擔心異常錯誤。C#Webmethod公共靜態無效的消息回Jquery
*重要的信息是count> 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 protected]"))
{
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;
所以從無效的返回類型更改爲您需要返回(字符串的新類的任何數據類型? ),並返回它。 –