简体   繁体   English

jQuery Ajax函数调用问题

[英]JQuery Ajax function call issue

I just started learning how to use jquery ajax in normal web forms in asp.net. 我刚刚开始学习如何在asp.net中以普通Web形式使用jquery ajax。 However i am stuck. 但是我被困住了。 I do not know what exactly is the issue. 我不知道到底是什么问题。 When i click the button on the form, it does not work the first time however sometimes it works only when clicked the second time. 当我单击表单上的按钮时,它第一次不起作用,但是有时它仅在第二次单击时才起作用。 and then stops working(Does not display the alert message in the success function ).It does not work even if i rebuild the project. 然后停止工作(在成功函数中不显示警报消息)。即使我重建项目,也无法工作。 No error is displayed in the console as well. 控制台中也不会显示任何错误。 Please help me. 请帮我。

Below is my code The employee class 下面是我的代码员工班

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; }
    }

The department class 部门课

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

The test.aspx code 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>

The code in the code behind file 文件后面代码中的代码

 [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();
    }

Please let me know where i am going wrong. 请让我知道我要去哪里错了。 Any help will be really appreciated. 任何帮助将不胜感激。

The code is fine and it is working for me. 代码很好,对我有用。

In your App_Start folder, within your RouteConfig.... 在您的App_Start文件夹中,在RouteConfig中...。

Comment out the following line or change it's RedirectMode: 注释掉以下行或将其更改为RedirectMode:

//settings.AutoRedirectMode = RedirectMode.Permanent; //settings.AutoRedirectMode = RedirectMode.Permanent;

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

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