私はjqueryが初めてです。私はしばしば C# でコーディングを行いますが、今は jquery を使用する必要があります。私はjqueryの基本を知っています。jqueryのほんの一部の機能。しかし、今、私はいくつかの事前のことをしたい. jqueryを使用して、asp.netページからSQLサーバーのデータを更新削除します。このためにjqueryコードを試しましたが、うまくいきません。エラーの内容とどこが間違っているのかわかりません。これが私が使用しているjqueryの私のコードです
<script type="text/javascript">
$(document).ready(function () {
$("#Button1").click(function () {
$.ajax({
type: 'POST',
contentType: "application/json;charset=utf-8",
url: 'Default2.aspx/InsertMethod',
data: "{'username':'" + document.getElementById('txtusername').value + "','password':'" + document.getElementById("txtpassword").value + "'}",
async: false,
success: function (response) {
$('#txtusername').val('');
$('#txtpassword').val('');
alert("record has been saved in database");
},
error: function () {
console.log('there is some error');
}
});
});
});
C#コードでWebメソッドを作成し、SQLサーバーにデータを保存するためのコードを記述し、そのWebメソッドをjqueryで呼び出します。しかし、このコードは機能していません これが私のC#コードです
[WebMethod]
public static string InsertMethod(string username, string password)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand("insert jquerydata values('" + username + "','" + password + "'", con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
return "true";
}
どうすればいいのか教えてください。