0

私は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";

    }

どうすればいいのか教えてください。

4

3 に答える 3

0

私はこれをテストしましたが、動作します:

Default2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default2.aspx.cs" Inherits="SO19542105.Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>SO19542105: how to save data in database using jquery</title>
    <script type="text/javascript" src="Scripts/jquery-1.8.2.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#Button1").click(function () {
                $.ajax({
                    type: "POST",
                    contentType: "application/json;charset=utf-8",
                    url: "Default2.aspx/InsertMethod",
                    data: "{'username':'" + $("#txtusername").val() + "', 'password':'" + $("#txtpassword").val() + "'}",
                    async: false,
                    success: function (response) {
                        $("#txtusername").val("");
                        $("#txtpassword").val("");
                        alert("record has been saved in database");
                    },
                    error: function () {
                        console.log("there is some error");
                    }
                });
            });
        });
    </script>
</head>
<body>
    <input type="text" id="txtusername" placeholder="username" />
    <input type="password" id="txtpassword" placeholder="password" />
    <input type="button" id="Button1" value="OK" />
</body>
</html>

Default2.aspx.cs

using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Services;

namespace SO19542105
{
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static string InsertMethod(string username, string password)
        {
            using(var con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString))
            using (var cmd = con.CreateCommand())
            {
                cmd.CommandText = "INSERT INTO jquerydata(username, password) VALUES(@username, @password)";
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Parameters.AddWithValue("@username", username);
                cmd.Parameters.AddWithValue("@password", password);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
            return "true";
        }
    }
}
于 2013-10-23T15:05:09.230 に答える
0

あなたのJqueryは問題ないようです。メソッドが呼び出されていることを確認してください。ブレーク ポイントを追加して、C# メソッドをデバッグできます。

あなたのC#コードで

SqlCommand cmd = new SqlCommand("insert jquerydata values('" + username + "','" + password + "'", con);

「into」キーワードが欠落していると思います。

また、'debugger' キーワードを使用して jquery をデバッグすることもできます。IE の [スクリプトのデバッグを無効にする] オプションのチェックを外してください。

于 2013-10-23T13:26:01.110 に答える