简体   繁体   English

在asp.net中使用jQuery Ajax将值插入数据库

[英]To insert values into database using jquery ajax in asp.net

In the below code i am trying to insert values into Database using jquery ajax.In my case i am calling the jquery ajax on button click .But it is not calling the webmethod.Pls help me to rectify this issue. 在下面的代码中,我尝试使用jquery ajax将值插入数据库。就我而言,我在按钮click上调用jquery ajax。但是它没有调用webmethod.Pls帮助我纠正此问题。

UnitDetails.aspx UnitDetails.aspx

  <script type="text/JavaScript" 
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
 function MyFunction() {

              $.ajax({
                  type: "POST",

                  url: "UnitDetails.aspx/InsertData",
                  data: "{'UnitType':'" + $("#<%=txtUnitTypeName.ClientID%>").val() + "','UnitTypeCode':'" + $("#<%=txtUnitTypeShortCode.ClientID%>").val() + "'}",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  async: "true",
                  cache: "false",
                  success: function (msg) {
                      alert("Success");
                      // On success                 
                  },
                  Error: function (x, e) {
                      alert("Fail");
                      // On Error
                  }
              });
          }  
<input name="data[UnitType][Name]" type="text" class="autofocus"maxlength="255" id="txtUnitTypeName" runat="server" />

<input name="data[UnitType][ShortCode]" type="text"  maxlength="5" id="txtUnitTypeShortCode"    runat="server" />



[WebMethod]
        public static void InsertData(string UnitType, string Code)
        {
            try
            {
                MastersClient Uinsert = new MastersClient();
                Dictionary<string, string> UnitVal = new Dictionary<string, string>();
                UnitVal.Add("UnitTypeName", UnitType.ToString());
                UnitVal.Add("UnitTypeShortCode", Code.ToString());
            }
            catch (Exception ex)
            {
                string strMsg = "Error message : " + Environment.NewLine + "Date : " + DateTime.Now.ToString("dd/MMM/yyyy HH:mm:ss") + Environment.NewLine + "Error : " + ex.Message + Environment.NewLine;
                Logger objErrorHandler = new Logger();
                objErrorHandler.MsgType = MessageType.MailAndEventLog;
                objErrorHandler.RaiseError(strMsg, ex);
            }
        }

Check your url address, maybe it should looks like that: 检查您的网址,也许看起来像这样:

~/UnitDetails.aspx/InsertData

or 要么

~/UnitDetails/InsertData/

and check data you are passing through, try to pass static data without extracting value by jQ maybe there is some error. 并检查您正在传递的数据,尝试传递静态数据而不通过jQ提取值,也许有一些错误。 Sth like that: 像这样:

{'UnitType': 'type', 'UnitTypeCode': 'typeCode'}

here's your solution 这是您的解决方案

     data: "{'UnitType':'" + $("#<%=txtUnitTypeName.ClientID%>").val() + "',
'UnitTypeCode':'" + $("#<%=txtUnitTypeShortCode.ClientID%>").val() + "'}",

your second argument in your posted data is UnitTypeCode 您发布的数据中的第二个参数是UnitTypeCode

while on your service your second argument is code which is wrong it should be same 在服务时,第二个参数是错误的code ,它应该是相同的

public static void InsertData(string UnitType, string Code)

should be 应该

public static void InsertData(string UnitType, string UnitTypeCode)

hope that helps ! 希望能有所帮助!

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

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