繁体   English   中英

使用JQuery进行Ajax调用

[英]Ajax calls using JQuery

我是第一次使用jquery进行此ajax调用。 我试图绑定数据库中的值。 我有一个数据访问层,数据是从那里返回的。 我正在尝试将此数据集绑定到我的.aspx页中的gridview。 我已经在.aspx页面中编写了ajax jquery调用,但出现“未定义”错误。

请指导我哪里出了问题。

我的代码是

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-2.1.4.js"></script>
    <script src="Scripts/jquery.validate.js"></script>
    <script>

        $(function () {


            $("#form1").validate({


                rules: {
                    txtName: "required",
                    txtCity: "required",
                    ddlDepName: "required",
                    txtSalary: {
                        required: true,
                        number: true
                    },

                },


                messages: {
                    txtName: "Please enter your Name",
                    txtCity: "Please enter your City",
                    ddlDepName: "Please select a department Name from the dropdown list",
                    txtSalary: {
                        required: "Please enter the Salary",
                        number: "Only Numbers are allowed"
                    }
                },

                submitHandler: function (form) {
                    form.submit();
                }
            });
            $('#btnInsertEmployee').click(function () {

                if ($("#form1").valid()) {

                }
            });
        });

        $(function () {
            GetEmployees();
        });

        function GetEmployees() {
            debugger;
            $.ajax({
                type: "POST",
                url: "DataAccessLayer.cs/GetEmployees",
                //data: '{pageIndex: ' + pageIndex + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function (response) {
                    alert(response.d);
                },
                error: function (response) {
                    alert(response.d);
                }
            });
        }

        function OnSuccess(response) {
            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            var customers = xml.find("EmployeesList");
            var row = $("[id*=GridView1] tr:last-child").clone(true);
            $("[id*=GridView1] tr").not($("[id*=GridView1] tr:first-child")).remove();
            $.each(EmployeesList, function () {
                var customer = $(this);
                $("td", row).eq(0).html($(this).find("EmployeeId").text());
                $("td", row).eq(1).html($(this).find("Name").text());
                $("td", row).eq(2).html($(this).find("City").text());
                $("td", row).eq(2).html($(this).find("DepartmentId").text());
                $("td", row).eq(2).html($(this).find("Salary").text());
                $("[id*=GridView1]").append(row);
                row = $("[id*=GridView1] tr:last-child").clone(true);
            });
            var pager = xml.find("Pager");
            $(".Pager").ASPSnippets_Pager({
                ActiveCssClass: "current",
                PagerCssClass: "pager",
                PageIndex: parseInt(pager.find("PageIndex").text()),
                PageSize: parseInt(pager.find("PageSize").text()),
                RecordCount: parseInt(pager.find("RecordCount").text())
            });
        };

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>

                    <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" >
                        <AlternatingRowStyle BackColor="#F7F7F7" />
                        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                        <SortedAscendingCellStyle BackColor="#F4F4FD" />
                        <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
                        <SortedDescendingCellStyle BackColor="#D8D8F0" />
                        <SortedDescendingHeaderStyle BackColor="#3E3277" />
                    </asp:GridView>
                </ContentTemplate>
            </asp:UpdatePanel>

    <table class="auto-style1">
                    <tr>
                        <td class="auto-style3">
                            <asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
                        </td>
                        <td class="auto-style4">
                            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td class="auto-style2">&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td class="auto-style2">
                            <asp:Label ID="lblCity" runat="server" Text="City"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td class="auto-style2">&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td class="auto-style2">
                            <asp:Label ID="lblDepName" runat="server" Text="Department Name"></asp:Label>
                        </td>
                        <td>
                            <asp:DropDownList ID="ddlDepName" runat="server" AutoPostBack="True" Height="16px" Width="122px">
                            </asp:DropDownList>
                        </td>
                    </tr>
                    <tr>
                        <td class="auto-style2">&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td class="auto-style2">
                            <asp:Label ID="lblSalary" runat="server" Text="Salary"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td class="auto-style2">&nbsp;</td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <asp:Button ID="btnInsertEmployee" runat="server" OnClick="btnInsertEmployee_Click" Text="Insert Employee" OnClientClick="return validate()" />
                            &nbsp;</td>
                    </tr>
                    <tr>
                        <td class="auto-style2" colspan="2">
                            <asp:Label ID="lblMessage" runat="server" ForeColor="Red"></asp:Label>
                        </td>
                    </tr>
                </table>
    </div>
    </form>
</body>
</html>
  1. 确保您的服务器正在返回数据:您已使用'POST''GET'数据...

  2. DataAccessLayer.cs/GetEmployees不是有效的网址。 要么调用网页(.aspx),要么调用Web方法,但是您没有从浏览器中调用DAL中的C#文件(.cs)...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM