J'essaie de sauvegarder des données en utilisant du soap Jquery avec C # WebMethod Mais ne peut pas save des données dans SQL Server s'il vous plaît aidez-moi comment puis-je sauvegarder des données en utilisant Soqu jquery . J'utilise IDE Visual Studio 2015 .
<script type="text/javascript"> function SavePersonRecord() { var Name = $.sortingm($('#<%=txtName.ClientID %>').val()); var LName = $.sortingm($('#<%=txtlname.ClientID %>').val()); var Company = $.sortingm($('#<%=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>
Vague question. Cependant, faites l'étape suivante:
Eh bien, comme je peux le voir, vous avez un problème avec votre appel ajax. CA devrait etre:
$.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 ssortingng) 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); } });
Au-dessus de votre webmethod vous devriez append:
[WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)]
Enfin, Mon code fonctionne bien Voici le code Il y a quelques changements dans l'application 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>