【问题标题】:inserting data in sql database without postback in asp.net在 sql 数据库中插入数据而不在 asp.net 中回发
【发布时间】:2016-06-23 16:30:20
【问题描述】:

当我尝试使用 AJAX 插入数据而不回发时,它没有插入。我在 ajaxinsert.aspx 页面中编写了 AJAX,并在相同的页面视图代码(即 ajaxinsert.aspx.cs)中编写了一个 webmethod。有什么问题?

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ajaxinsert.aspx.cs" Inherits="ajaxweb.ajaxinsert" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" >
        $(document).ready(function () {
            $("#insert").click(function (e) {
                e.preventDefault()
                var Name = $("#name1").val();
                $.ajax({
                    type: "post",
                    dataType: "json",
                    url: "Contact.aspx/savedata",
                    contentType: "application/json; charset=utf-8",
                    data: { studentname: Name },
                    success: function () {
                        $("#divreslut").text("isnerted data");
                    },
                    error: function () {
                        alert("not inseted");
                    }
                });
            });
        });
    </script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input type="text" id="name1" name="name1" />
            <input type="submit" id="insert" value="isnertdata" />
        </div>
    </form>
    <div id="divreslut"></div>
</body>
</html>
[WebMethod]
public static void savedata(string studentname)
{
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("sp_savedata", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@name", studentname);

            //if (con.State == ConnectionState.Closed)
            //{
            con.Open();

            Int32 retVal = cmd.ExecuteNonQuery();
            if (retVal > 0)
            {
                Console.WriteLine("inserted sucess");
            }
            //if (retVal > 0)
            //{
            //    status = true;
            //}
            //else
            //{
            //    status = false;
            //}
            //return status;
        }
    }

【问题讨论】:

    标签: jquery sql asp.net asp.net-ajax


    【解决方案1】:

    请在上面的 .aspx 页面代码中修改以下行

    data: { studentname: Name },
    

    data: JSON.stringify({studentname: Name }), 
    

    Javascript 代码

     <script type="text/javascript" >
            $(document).ready(function () {
                $("#insert").click(function (e) {
                    e.preventDefault();
                    var Name = $("#name1").val();
                    $.ajax({
                        type: "post",
                        dataType: "json",
                        url: "Contact.aspx/savedata",
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify({studentname: Name }),
                        success: function () {
                            $("#divreslut").text("isnerted data");
                        },
                        error: function () {
                            alert("not inseted");
                        }
                    });
                });
            });
        </script>
    

    【讨论】:

    • 添加数据后:JSON.stringify({studentname: Name }),它没有命中网络方法@deepak
    • 你能检查你的代码吗?我相信它会工作
    • 正在调试我的代码,但它没有达到@deepak 方法
    • 好的,你能告诉我“Contact.aspx”和“ajaxinsert.aspx”都在同一个文件夹中还是它们是不同的子文件夹?
    【解决方案2】:
    【解决方案3】:

    Deepak 回答了一半。您需要将processData: false, 添加到ajax 参数。正确的javascript是:

    $(document).ready(function () {
        $("#insert").click(function (e) {
            e.preventDefault()
            var Name = $("#name1").val();
            $.ajax({
                type: "post",
                dataType: "json",
                url: "Contact.aspx/savedata",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({ studentname: Name }),
                processData: false,
                success: function () {
                   $("#divreslut").text("inserted data");
                },
                error: function () {
                   alert("not inserted");
               }
            });
        });
    });
    

    如果没有 processData: false,ajax 方法会尝试将数据转换为 URL 参数。

    顺便说一句,调试 AJAX 帖子问题的最佳方法是使用浏览器中 F12 开发人员工具 UI 上的“网络”选项卡。使用该工具,您可以看到 JSON 数据正在转换为 studentname=Bob,这应该会引导您找到正确的解决方案。

    【讨论】:

      【解决方案4】:
      Add jQuery Library:
      
          First we have to add jQuery library. You can download jQuery library here, or you can use following jQuery CDN.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多