2014-10-12 62 views
0

我目前正在嘗試使用jQuery Ajax,但有一個小問題,並會很高興收到一點幫助!Jquery ajax插入語句

我想用jquery ajax和web服務將數據插入到數據庫中。

我的HTML看起來像這樣:

<title></title> 
<script src="Scripts/jquery-2.1.1.intellisense.js"></script> 
<script src="Scripts/jquery-2.1.1.js"></script> 
<script> 
    $(document).ready(function() { 
    $("#loadingPanel").hide(); 
    $("#submitBtn").click(function() { 
     var fName = $("#fnameTextBox").val(); 
     var lName = $("#lnameTextBox").val(); 
     var Age = $("#ageTextBox.ClientID").val(); 
     $("#loadingPanel").show(); 

     $.ajax({ 
     url: "demoService.asmx/addInfo", 
     type: "POST", 
     dataType: "json", 
     contentType: "application/json; charset=utf-8", 
     data: JSON.stringify({ 
      "fName": fName, 
      "lName": lName, 
      "Age": Age 
     }), 
     success: function(data) { 
      if (data.d == "success") { 
      alert("Data saved"); 
      var firstName = $('#<%=fnameTextBox.ClientID%>').val(''); 
      var lastName = $('#<%=lnameTextBox.ClientID%>').val(''); 
      var age = $('#<%=ageTextBox.ClientID%>').val(''); 

      } 
     }, 
     error: function(xhr, ajaxOptions, thrownError) { 
      alert(thrownError); 
     } 
     }).done(function() { 
     $("#loadingPanel").hide(); 
     }); 
    }); 
    }); 
</script> 
</head> 

<body> 

    <form id="form1" runat="server"> 
    <div> 
     <table> 
     <tbody> 
      <tr> 
      <td>First Name</td> 
      <td> 
       <asp:TextBox runat="server" ID="fnameTextBox" Width="200px" /> 
      </td> 
      </tr> 
      <tr> 
      <td>Last Name</td> 
      <td> 
       <asp:TextBox runat="server" ID="lnameTextBox" Width="200px" /> 
      </td> 
      </tr> 
      <tr> 
      <td>Age</td> 
      <td> 
       <asp:TextBox runat="server" ID="ageTextBox" Width="200px" /> 
      </td> 
      </tr> 
      <tr> 
      <td> 
       <input type="button" value="Submit" id="submitBtn" /> 
       <div id="loadingPanel" style="color:green; font-weight:bold;">Data saving...</div> 
      </td> 

      </tr> 
     </tbody> 
     </table> 
    </div> 
    </form> 
</body> 

這樣

[WebMethod] 
     public string addInfo(string fName, string lName, int Age) 
     { 
      string status = ""; 

      infoTable info = new infoTable {FirstName = fName, LastName = lName, age = Age }; 

      using(demoDBEntities demoEntity = new demoDBEntities()) 
      { 
       demoEntity.infoTable.Add(info); 
       demoEntity.SaveChanges(); 
       status = "success"; 
      } 

      return status; 
     } 

我的web服務當我按下提交按鈕和內部服務器錯誤發生時,當我看到內部異常,它說雖然我在文本框中輸入了namne,但沒有提供參數fName。請幫忙嗎?

錯誤:

System.InvalidOperationException: missing parameter: fName. 
System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection) 
    System.Web.Services.Protocols.UrlParameterReader.Read(HttpRequest request) 
System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() 
    System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest() 
+0

請不要在評論中發佈代碼轉儲... [編輯]問題並在那裏更新... – 2014-10-12 09:22:43

+0

嘗試清除你的包裝'JSON.stringify({})'圍繞你的參數,只留下參數聲明。 – 2014-10-12 09:26:32

+0

你有沒有在asmx中使用'[System.Web.Script.Services.ScriptService]'來啓用js訪問web服務? – 2014-10-12 09:28:52

回答

0

你需要傳遞data阿賈克斯如下。

data: "{ 'fName': '" + fName+"', 'lName' : '"+ lName +"', 'Age' : '" + Age+ "'}"

另外,你需要在Web服務類的聲明使用[System.Web.Script.Services.ScriptService]

+0

它的工作!警告的數據保存,但是當我在SQL管理工作室看到沒有新的原料被添加到表 – user3590965 2014-10-12 09:48:49

+0

是否有任何約束設置在表?這可能會阻止數據插入。 – 2014-10-12 09:50:23

+0

我檢查了它,數據被插入爲udefined。請幫忙嗎? – user3590965 2014-10-12 21:00:49