[英]JQuery AutoComplete TextBox from database using AJAX PageMethods in ASP.Net is not Working in Internet Explorer
[英]To insert values into database using jquery ajax in asp.net
在下面的代碼中,我嘗試使用jquery ajax將值插入數據庫。就我而言,我在按鈕click上調用jquery ajax。但是它沒有調用webmethod.Pls幫助我糾正此問題。
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);
}
}
檢查您的網址,也許看起來像這樣:
~/UnitDetails.aspx/InsertData
要么
~/UnitDetails/InsertData/
並檢查您正在傳遞的數據,嘗試傳遞靜態數據而不通過jQ提取值,也許有一些錯誤。 像這樣:
{'UnitType': 'type', 'UnitTypeCode': 'typeCode'}
這是您的解決方案
data: "{'UnitType':'" + $("#<%=txtUnitTypeName.ClientID%>").val() + "',
'UnitTypeCode':'" + $("#<%=txtUnitTypeShortCode.ClientID%>").val() + "'}",
您發布的數據中的第二個參數是UnitTypeCode
在服務時,第二個參數是錯誤的code
,它應該是相同的
public static void InsertData(string UnitType, string Code)
應該
public static void InsertData(string UnitType, string UnitTypeCode)
希望能有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.