【问题标题】:How Can I save Data using Soap Jquery in SQL Server 2012? [closed]如何在 SQL Server 2012 中使用 Soap Jquery 保存数据? [关闭]
【发布时间】:2017-11-22 12:53:03
【问题描述】:

我正在尝试使用带有 C# WebMethod 的肥皂 Jquery 保存数据,但无法在 SQL Server 中保存数据,请帮助我如何才能使用 Soap jquery 保存数据。 我正在使用 IDE Visual Studio 2015

<script type="text/javascript">  

    function SavePersonRecord() {  

        var Name = $.trim($('#<%=txtName.ClientID %>').val());  
        var LName = $.trim($('#<%=txtlname.ClientID %>').val());
        var Company = $.trim($('#<%=txtCompany.ClientID %>').val());    
        var Messege = "";           
        if (Name == '') {  
            Messege = "Can not Blank Name";
        }          
        if (LName == '') {  
            Messege += "Can not Blank Last Name";  
        }  
        if (Company == '') {
            Messege += " Company Name Can not Blank";
        }
        if (Messege.length == 0) {        
            $.ajax({  
                type: "POST",  
                dataType: "json",  
                contentType: "application/json; charset=utf-8",  
                url: "Soap-Service.aspx/InsertPersonRecord",
                data: "{'FirstName':'" + Name + "', 'LastName':'" + LName + "','Company':'" + Company + "'}",
                success: function (Record) {  

                        $('#txtName').val();  
                        $('#txtlName').val();  
                        $('#txtCompany').val();

                    if (Record.d == true) {  

                        $('#Result').text("Your Record insert");  
                    }  
                    else {  
                        $('#Result').text("Your Record Not Insert");  
                    }  

                },  
                Error: function (textMsg) {  

                    $('#Result').text("Error: " + Error);  
                }  
            });  
        }  
        else {             
            $('#Result').html('');  
            $('#Result').html(Messege);  
        }  
        $('#Result').fadeIn();  
    }  
    </script>  





   <table border="0" cellpadding="5" cellspacing="5" style="border: solid 2px Green;">
                                <tr>
                                    <td colspan="2" style="background-color: red; color: white; font-weight: bold; font-size: 12pt; text-align: center; font-family: Verdana;">Enter Employee Information</td>
                                </tr>
                                <tr>
                                    <td>First Name:  
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtName" runat="server" Text="" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>Last Name:  
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtlname" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>Company  
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtCompany" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td></td>
                                    <td>
                                        <asp:Button ID="btnInsertRecord" Text="Save" runat="server" OnClientClick="SavePersonRecord(); return false" />
                                    </td>
                                </tr>
                            </table>

【问题讨论】:

  • 这个问题如何获得投票?请描述您的努力和具体问题。
  • @RomanoZumbé 正是我想知道的,一些代码(您尝试过的任何代码)也会有所帮助。

标签: javascript c# jquery asp.net sql-server


【解决方案1】:

模糊的问题。但是,请执行以下步骤:

  1. 您是否在浏览器控制台中遇到任何错误,从而破坏了您的 javascript 代码?
  2. 您是否在“错误:”部分捕捉到任何错误?
  3. 如果以上两个看起来都不错,你有没有在你的 web 方法中放入 dubugger 来检查它是否被命中?
  4. 在这一切之后,是否存在任何服务器端异常(在您的 Web 方法中)?
  5. 4) 的答案是肯定的,发布例外情况

【讨论】:

  • 不,先生,我没有收到任何错误,但使用调试器显示 javascript 错误字符串不是 JSON 格式
  • 这里有什么问题,只是一个疯狂的猜测?数据:"{'FirstName':'" + Name + "', 'LastName':'" + LName + "','Company':'" + Company + "'}"
  • 先生问题在这里显示 URL:“Soap-Service.aspx/SaveUser”,(字符串不是 JSON 格式)我正在使用另一个示例的相同脚本它工作正常,但是当我创建新网站时在 Visual Studio 2015 中的表单项目然后显示此问题,我正在共享另一个脚本此脚本也显示相同的错误,但在另一个项目示例中它工作正常..
【解决方案2】:

好吧,我可以看到您的 ajax 调用有问题。应该是:

      $.ajax({  
            type: "POST",  
            dataType: "json",  
            contentType: "application/json; charset=utf-8",  
            url: "Soap-Service.aspx/SaveUser", //Here you got wrong method
            //right here you should pass object (not string) with right property names (like you have in your web method)
            data: { FName: Name, LNAme: LName, Company: Company},
            success: function (Record) {  
                $('#txtName').val();  
                $('#txtlName').val();  
                $('#txtCompany').val();

                if (Record.d == true) {  

                    $('#Result').text("Your Record insert");  
                }  
                else {  
                    $('#Result').text("Your Record Not Insert");  
                }  
            },  
            error: function (textMsg) {  
                $('#Result').text("Error: " + Error);  
            }  
        }); 

您应该在您的网络方法上方添加:

[WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)]

【讨论】:

    【解决方案3】:

    最后,我的代码运行好了,这里是代码 Web 应用程序有一些变化

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
        <script type="text/javascript">
            $(function () {
                $('#btnSubmit').click(function () {
                    var Name = $('#txtname').val();
                    var Contact = $('#txtcontact').val();
                    //var body = $('#txtbody').val();
                    if (Name != '' && Contact != '') {
                        $.ajax({
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            url: "Soap-Jquery.aspx/InsertData",
                            data: "{'EmpName':'" + Name + "','ConNo':'" + Contact + "'}",
                            dataType: "json",
                            success: function (data) {
                                var obj = data.d;
                                if (obj == 'true') {
                                    $('#txtname').val('');
                                    $('#txtcontact').val('');
                                    //$('#txtbody').val('');
                                    $('#lblmsg').html("Details Submitted Successfully");
                                    window.location.reload();
                                }
                            },
                            //error: function (result) {
                            //    alert("Error");
                            //}
                        });
                    }
                    else {
                        alert('Please enter all the fields')
                        return false;
                    }
                })
            });
        </script>
    <asp:Panel ID="pnl_add_payment" runat="server">
        
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    
        <section>
    
            <div class="container">
                <div class="row">
                    <div class="form-group">
                        <button type="button" class="btn btn-primary ribbon">Data Insert Using Jquery</button>
                        <asp:Label ID="lblmsg" runat="server" Text="" ForeColor="Red"></asp:Label>
                        <div class="col-md-12" style="margin-top: 15px;">
                            <div class="col-md-2">Enter Name</div>
                            <div class="col-md-4">
                              
                                  <asp:TextBox ID="txtname" runat="server" class="form-control" placeholder="Enter Name"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtname" ErrorMessage="*" ForeColor="Red" ValidationGroup="a"></asp:RequiredFieldValidator>
                              
                            </div>
                            <div class="col-md-2">Enter Contact No</div>
                            <div class="col-md-4">
                                <asp:TextBox ID="txtcontact" runat="server" class="form-control" placeholder="Enter Contact No"></asp:TextBox>
                                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtcontact" runat="server" ErrorMessage="Only Numbers allowed" ValidationExpression="\d+" ForeColor="Red" ValidationGroup="a"></asp:RegularExpressionValidator>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtcontact" ErrorMessage="*" ForeColor="Red" ValidationGroup="a"></asp:RequiredFieldValidator>
                            </div>
                            <div class="col-md-6">
                                <asp:Button ID="btnSubmit" runat="server" Text="Submit" value="Submit"  ValidationGroup="a" />
                            </div>
    
                        </div>
    
                    </div>
                    
                    <%--End Form Group--%>
                </div>
                <%--End Row--%>
            </div>
            <%--End Container--%>
        </section>    
        <br /><br /> 
        <section>
            <div class="container">
                <div class="row">
                    <div class="col-md-6">
    
    
                        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="100%">
    
                            <Columns>
                                <asp:BoundField DataField="EmpName" HeaderText="Name" />
                                <asp:BoundField DataField="ConNo" HeaderText="Number" />
                            </Columns>
                            <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                            <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                            <RowStyle BackColor="White" ForeColor="#330099" />
                            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                            <SortedAscendingCellStyle BackColor="#FEFCEB" />
                            <SortedAscendingHeaderStyle BackColor="#AF0101" />
                            <SortedDescendingCellStyle BackColor="#F6F0C0" />
                            <SortedDescendingHeaderStyle BackColor="#7E0000" />
                            
                        </asp:GridView>
                    </div>
                </div>
            </div>
        </section>
    
    </asp:Panel>

    [网络方法] 公共静态字符串 InsertData(字符串 EmpName,字符串 ConNo) { 字符串味精=字符串。空; 使用 (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString)) { 使用 (SqlCommand cmd = new SqlCommand("sp_insertEmp", con)) { con.Open(); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@EmpName", EmpName); cmd.Parameters.AddWithValue("@ConNo", ConNo); int i = cmd.ExecuteNonQuery(); con.Close(); 如果 (i == 1) {

                        msg = "True";
                    }
                    else
                    {
                        msg = "false";
                    }
    
                }
            }
    
            return msg;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-26
      • 2013-01-13
      • 2013-08-19
      • 1970-01-01
      • 2013-08-23
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多