2015-06-09 35 views
0

嗨我需要返回一個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; 
+2

所以從無效的返回類型更改爲您需要返回(字符串的新類的任何數據類型? ),並返回它。 –

回答

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 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) 
       { 
        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; 
}); 
+0

thnx您的帖子!事情發生的幾件事(不是)首先返回catch事件異常,其次返回新消息警報是未定義的。儘管我通過返回和成功返回了消息,但您已經獲得了幫助:function(result){msg = result.hasOwnProperty(「d」)? result.d:result; alert(msg); }, – tuvboy

+0

只是'console.log(data)',看看你得到了什麼...... –

+0

[object Object] {...} d「{」Message「:」電子郵件插入「}」 – tuvboy