【问题标题】:JQuery Ajax function call issueJQuery Ajax 函数调用问题
【发布时间】:2017-03-31 20:15:16
【问题描述】:

我刚开始学习如何在 asp.net 的普通 Web 表单中使用 jquery ajax。但是我被卡住了。我不知道到底是什么问题。 当我单击表单上的按钮时,它第一次不起作用,但有时它仅在第二次单击时才起作用。 然后停止工作(不在成功功能中显示警报消息)。即使我重建项目也不起作用。控制台中也没有显示错误。请帮帮我。

下面是我的代码 员工类

public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string Country { get; set; }
        public int Salary { get; set; }
        public int DeptId { get; set; }
    }

系类

public class Department
    {
        public int DeptId { get; set; }
        public string Name { get; set; }
    }

test.aspx 代码

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type = "text/javascript">
    $(document).ready(function () {
        $('#Button1').click(function() {
            var Name = $("#txtName").val();
            var Gender = $("#ddlGender").val();
            var Country = $("#txtCountry").val();
            var Salary = $("#txtSalary").val();
            var DeptId = $("#ddlDept").val();
            var employee = {
                "Name": Name,
                "Gender": Gender,
                "Country": Country,
                "Salary": Salary,
                "DeptId": DeptId
            }
            $.ajax({
                context: this,
                type: "POST",
                url: "test.aspx/GetResultFromDB",
                data: JSON.stringify({ employee: employee }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                cache:false,
                success: OnSuccess,
                failure: function (response) {
                    alert("In Error");
                    alert(response.d);
                }
            });
        });
        function OnSuccess(response) {
            alert("In succcess");
            alert(response.d);
            console.log(response.d);
        }
    });
</script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <div>
                <table>
            <tr>
                <td>Name</td>
                <td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Country</td>
                <td><asp:TextBox ID="txtCountry" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Gender</td>
                <td><asp:DropDownList ID="ddlGender" runat="server">
                    <asp:ListItem Text="Select" Value="0"></asp:ListItem>
                    <asp:ListItem Text="Male" Value="Male"></asp:ListItem>
                    <asp:ListItem Text="Female" Value="Female"></asp:ListItem>
                    </asp:DropDownList></td>
            </tr>
            <tr>
                <td>Salary</td>
                <td>
                    <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Department</td>
                <td><asp:DropDownList ID="ddlDept" runat="server"></asp:DropDownList></td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="Button1" runat="server" Text="Button"/>
                </td>
            </tr>
        </table>
            </div>
        </div>
    </form>
</body>
</html>

代码隐藏文件中的代码

 [System.Web.Services.WebMethod]
    public static string GetResultFromDB(Employee employee)
    {
        SqlConnection con = EmpDeptDataLayer.GetDBConnection();
        SqlCommand cmd = new SqlCommand("spAddEmployee", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@Name", employee.Name));
        cmd.Parameters.Add(new SqlParameter("@Country", employee.Country));
        cmd.Parameters.Add(new SqlParameter("@Gender", employee.Gender));
        cmd.Parameters.Add(new SqlParameter("@Salary", employee.Salary));
        cmd.Parameters.Add(new SqlParameter("@DeptId", employee.DeptId));
        cmd.Parameters.Add(new SqlParameter("@Result", SqlDbType.VarChar, 50)).Direction = ParameterDirection.Output;
        con.Open();
        int count = cmd.ExecuteNonQuery();
        con.Close();
        string Result = "";
        if (count > 0)
        {
             Result = cmd.Parameters["@Result"].Value.ToString();
        }
        else
        {
            Result = "Error!! Something went wrong";
        }
        return Result.ToString();
    }

请让我知道我哪里出错了。 任何帮助将不胜感激。

【问题讨论】:

    标签: c# jquery asp.net .net ajax


    【解决方案1】:

    代码很好,对我有用。

    在 App_Start 文件夹中,在 RouteConfig....

    注释掉以下行或将其更改为 RedirectMode:

    //settings.AutoRedirectMode = RedirectMode.Permanent;

    【讨论】:

    • 感谢您帮助我。但是我在 Route.Config 文件中没有这样的行。我什至创建了一个空项目并尝试了同样的方法,但它仍然无法正常工作
    猜你喜欢
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    相关资源
    最近更新 更多