我有一个c#(。net4)代码的表单。在此表格中,用户填写他的说明并提交 我想在jquery中使用ajax或post方法来防止blink.i编写流动的代码。 “success”函数执行但它不起作用并且在数据库中插入任何记录;我单独执行executemember方法。它没有问题,但它不能用于jquery ajax.where有问题吗?
[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>
感谢
答案 0 :(得分:1)
答案 1 :(得分:1)
检查出来:
$(document).ready(function () {
$("#Button1").click(function () {
var userData = new Object();
userData.username = $('#<%=TextBox1.ClientID%>').val();
userData.name = $('#<%=TextBox2.ClientID%>').val();
userData.family = $('#<%=TextBox3.ClientID%>').val();
userData.password = $('#<%=TextBox4.ClientID%>').val();
userData.email = $('#<%=TextBox5.ClientID%>').val();
userData.tel = $('#<%=TextBox6.ClientID%>').val();
userData.codemeli = $('#<%=TextBox7.ClientID%>').val();
$.ajax({
type: "POST",
url: "WebApplication20.aspx/executeinsert",
data: userData,
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);
}
});
});
});
P.S。如果它调用的话,尝试调试并在web方法中放置断点。
答案 2 :(得分:0)
json
格式不正确'name':name
未包含在'
中,string
。
data: "{'username' : 'username', 'name':name,'family':family,'password':password,'email':email,'tel':tel,'codemeli':codemeli }",
应该是
data: "{'username' : 'username', 'name':'name', 'family':'family', 'password':'password', 'email':'email', 'tel':'tel', 'codemeli':'codemeli'}",