2016-11-17 74 views
0

我剛開始學習如何在asp.net中的正常web表單中使用jquery ajax。但是我卡住了。我不知道究竟是什麼問題。 當我單擊窗體上的按鈕時,它不會在第一次運行,但有時它只在第二次單擊時纔有效。 然後停止工作(不會在成功功能中顯示警告消息)。即使重建項目,它也不起作用。控制檯中也不會顯示錯誤。請幫幫我。JQuery Ajax函數調用問題

下面是我的代碼 Employee類

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

Department類

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

請讓我知道我要去哪裏錯了。 任何幫助將非常感激。

回答

0

該代碼是好的,它爲我工作。

在你App_Start文件夾,您RouteConfig內....

註釋掉以下兩行或更改其RedirectMode:

//settings.AutoRedirectMode = RedirectMode.Permanent;

+0

謝謝你試圖幫助我。但是我在Route.Config文件中沒有這樣的行。我甚至創建了一個空的項目,並嘗試相同的,但它仍然不工作 – Videl