一直在嘗試對正確的輸入和服務器驗證進行jquery驗證,以檢查用戶/電子郵件是否存在。唯一的問題是參數並不總是從客戶端發送出於某種原因,即使這通過了jQuery驗證。 嘗試在服務器端方法的開始處輸入中斷點,很少進入那裏,在將參數插入參數後看到json看起來好了就放置了警報。 即時通訊使用jquery,asp.net,c#。無法將數據從jquery發送到c#服務器
客戶端代碼:
var user = $("#txt_register_username").val();
var password = $("#txt_register_password").val();
var firstname = $("#txt_register_firstname").val();
var lastname = $("#txt_register_lastname").val();
var birthdate = $("#txt_register_birthdate").val();
var picture = $("#txt_register_picture").val();
var car = $("#txt_register_car").val();
var email = $("#txt_register_email").val();
var home = $("#txt_register_home").val();
var cell = $("#txt_register_cell").val();
var params = "{username:" + user
+ ",firstname:" + firstname
+ ",lastname:" + lastname
+ ",birthdate:" + birthdate
+ ",pic:123"
+ ",carowned:" + car
+ ",email:" + email
+ ",password:" + password
+ ",home:" + home
+ ",cell:" + cell
+ "}";
$.ajax
({
type: "POST",
url: "Register.aspx/AddUser",
data: params,
contentType: "application/json; charset=utf-8",
dateType: "json",
success: function (result) {
alert(result.d);
}
});
知情同意稍後將二進制的,所以它只是暫時的,忽略它是字符串。
服務器代碼:
[WebMethod]
public static string AddUser(string username, string firstname, string lastname, string birthdate, string pic, string carowned, string email, string password, string home, string cell)
{
DataSet1TableAdapters.UsersTableAdapter userAdapter = new DataSet1TableAdapters.UsersTableAdapter();
DataSet1.UsersDataTable userTable = new DataSet1.UsersDataTable();
userAdapter.Fill(userTable);
int index = 0;
Console.WriteLine("entering valiation area");
foreach (DataRow dr in userTable.Rows)
{
if (userTable.Rows[index]["username"].Equals(username))
{
Console.WriteLine("a user who already exists tried to create an account");
return "user already exists with that username";
}
if (userTable.Rows[index]["email"].Equals(email))
{
Console.WriteLine("a user who already exists with an email tried to create an account");
return "user already exists with that email";
}
index++;
}
try
{
userAdapter.Insert(username, firstname, lastname, birthdate, pic, carowned, email, password, home, cell);
Console.WriteLine("new user added to db");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return "oops , something went wrong.";
}
return "new user added to database.";
}
你會得到什麼錯誤。顯示錯誤? – 2013-05-07 09:31:58
顯示你的按鈕和jQuery代碼(按鈕點擊) – 2013-05-07 09:35:56