我有一個帶有c#(.net4)代碼的表單。在這種形式下,用戶填寫他的規格並提交。
我想在jquery中使用ajax或post方法來預防blink.i寫流程代碼。 「成功」函數執行,但它不工作,任何記錄插入數據庫;我執行executemember方法separate.it工作沒有問題,但它不工作與jQuery的ajax.where是問題嗎?用jqueryajax在數據庫中插入數據
[WebMethod]
public static string executeinsert(string name ,string family , string username,string password , string email,string tel, string codemeli)
{ string constring = "data source=.;database=site;integrated security=true;";
SqlConnection con = new SqlConnection(constring);
SqlCommand com = new SqlCommand("insertmember", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add(new SqlParameter("@username", SqlDbType.NVarChar, 250));
com.Parameters["@username"].Value = username;
com.Parameters.Add(new SqlParameter("@name", SqlDbType.NVarChar, 150));
com.Parameters["@name"].Value = name;
com.Parameters.Add(new SqlParameter("@password", SqlDbType.NVarChar, 50));
com.Parameters["@password"].Value = password;
com.Parameters.Add(new SqlParameter("@family", SqlDbType.NVarChar, 250));
com.Parameters["@family"].Value = family;
com.Parameters.Add(new SqlParameter("@email", SqlDbType.NVarChar, 50));
com.Parameters["@email"].Value = email;
com.Parameters.Add(new SqlParameter("@codemeli", SqlDbType.NChar, 10));
com.Parameters["@codemeli"].Value = codemeli;
com.Parameters.Add(new SqlParameter("@tel", SqlDbType.NChar, 12));
com.Parameters["@tel"].Value = tel;
con.Open();
com.ExecuteNonQuery();
con.Close();
return "success";
}
和我的jQuery代碼
<script type="text/javascript">
$(document).ready(
function() {
$("#Button1").click(
function() {
var username, family, name, email, tel, codemeli, password;
username = $('#<%=TextBox1.ClientID%>').val();
name = $('#<%=TextBox2.ClientID%>').val();
family = $('#<%=TextBox3.ClientID%>').val();
password = $('#<%=TextBox4.ClientID%>').val();
email = $('#<%=TextBox5.ClientID%>').val();
tel = $('#<%=TextBox6.ClientID%>').val();
codemeli = $('#<%=TextBox7.ClientID%>').val();
$.ajax(
{
type: "POST",
url: "WebApplication20.aspx/executeinsert",
data: "{'username':'username','name':name,
'family':family,'password':password,
'email':email,'tel':tel,
'codemeli':codemeli}",
contentType: "application/json;charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function(msg) {
alert(msg);
},
error: function (x, e) {
alert("The call to the server side failed. "
+ x.responseText);
}
}
);
}
)
})
</script>
感謝
你想用'data:「{'user ...'line實現什麼?是傳遞一個json字符串還是一組鍵/值對? – Musa 2012-07-06 06:13:58
我不確定它是問題,但你指出的數據類型爲JSON把提供的字符串數據屬性 – Samich 2012-07-06 06:21:17
我現在看到它是在json的嘗試,但它看起來不像WebMethod使用json – Musa 2012-07-06 06:32:44