【发布时间】:2015-10-20 20:38:56
【问题描述】:
在下面的代码中,我有一个文本框。我的目标是当我专注于文本框时,它将通过 ajax 调用服务器端代码。但是我得到一个错误未知的 web 方法txtField_GotFocus 参数名称方法名称。请帮我纠正错误。
设计代码:
$(document).ready(function () {
$("#<%=txtField.ClientID%>").bind("focus", function () {
$.ajax({
type: "POST",
url: "<%=Request.FilePath%>/txtField_GotFocus",
data: "{}",
contentType: "application/json",
dataType: "json",
success: function (msg) {
//alert("success message here if you want to debug");
},
error: function (xhr) {
var rawHTML = xhr.responseText;
var strBegin = "<" + "title>";
var strEnd = "</" + "title>";
var index1 = rawHTML.indexOf(strBegin);
var index2 = rawHTML.indexOf(strEnd);
if (index2 > index1) {
var msg = rawHTML.substr(index1 + strBegin.length, index2 - (index1 + strEnd.length - 1));
alert("error: " + msg);
} else {
alert("General error, check connection");
}
}
});
});
});
<asp:TextBox ID="txtField" runat="server" AutoPostBack="true" OnTextChanged="txtField_TextChanged" ClientIDMode="Static"></asp:TextBox>
field.ascx:
public static void txtField_GotFocus()
{
string foo = HttpContext.Current.Request["foo"];
//code...
}
【问题讨论】:
-
方法上是否需要 WebMethod 属性?
-
您的 txtField_GotFocus 方法是静态的。不能通过 ajax 调用静态方法...
-
显示txtField_GotFocus
参数名方法名 -
@Jason WebMethos 必须是
staticstackoverflow.com/questions/18463189/…