ajax 未调用 url 中的方法
ajax not calling method in url
这是我第一次使用 ajax,所以我不确定哪里出错了。
当下拉列表更改时,我在 javascript 中调用一个方法,我想在代码隐藏中调用一个方法。
这是下拉列表的标记:
<asp:DropDownList ID="ddListSubject" runat="server" ClientIDMode="Static" onchange="SubjectChanged()" CssClass="chosen-single">
</asp:DropDownList>
这是调用的 javascript 函数 'SubjectChange()'。
function SubjectChanged() {
var strSubject = document.getElementById("ddListSubject").value;
if (strSubject == "Custom") {
document.getElementById("txtBoxSubject").value = "";
document.getElementById("txtBoxSubject").focus();
}
else {
document.getElementById("txtBoxSubject").value = strSubject;
}
ShowMaxMsgLength();
CountChars();
}
函数 'ShowMaxMsgLength()' 包含 ajax 代码以在代码隐藏中调用方法:
function ShowMaxMsgLength() {
$.ajax({
type: "POST",
url: "Default.aspx/GetMaxMsgLength",
data: "{'id': '1'}",
contentType: "application/json; charset=utf-8",
datatype: "json",
success: OnSuccess,
failure: OnFailure,
error: function (exception) { alert('Exception:' + exception); }
});
};
这是代码隐藏中的 GetMaxMsgLength() 方法:
public static string GetMaxMsgLength(int id)
{
string tstrMaxMsgLength = string.Empty;
return tstrMaxMsgLength = "32";
}
我只是想return'32'暂时看看是不是运行这个方法。
我知道 ShowMaxMsgLength() 被调用是因为我在里面放了一个 'alert'。
存在异常 returned:'Exception: [object Object]'。
我不知道我没有设置什么导致这个异常。
谢谢。
这是只有新手才会错过的东西...
添加
[System.Web.Services.WebMethod] 方法之前的属性,GetMaxMgsLength。
这是我第一次使用 ajax,所以我不确定哪里出错了。 当下拉列表更改时,我在 javascript 中调用一个方法,我想在代码隐藏中调用一个方法。 这是下拉列表的标记:
<asp:DropDownList ID="ddListSubject" runat="server" ClientIDMode="Static" onchange="SubjectChanged()" CssClass="chosen-single">
</asp:DropDownList>
这是调用的 javascript 函数 'SubjectChange()'。
function SubjectChanged() {
var strSubject = document.getElementById("ddListSubject").value;
if (strSubject == "Custom") {
document.getElementById("txtBoxSubject").value = "";
document.getElementById("txtBoxSubject").focus();
}
else {
document.getElementById("txtBoxSubject").value = strSubject;
}
ShowMaxMsgLength();
CountChars();
}
函数 'ShowMaxMsgLength()' 包含 ajax 代码以在代码隐藏中调用方法:
function ShowMaxMsgLength() {
$.ajax({
type: "POST",
url: "Default.aspx/GetMaxMsgLength",
data: "{'id': '1'}",
contentType: "application/json; charset=utf-8",
datatype: "json",
success: OnSuccess,
failure: OnFailure,
error: function (exception) { alert('Exception:' + exception); }
});
};
这是代码隐藏中的 GetMaxMsgLength() 方法:
public static string GetMaxMsgLength(int id)
{
string tstrMaxMsgLength = string.Empty;
return tstrMaxMsgLength = "32";
}
我只是想return'32'暂时看看是不是运行这个方法。 我知道 ShowMaxMsgLength() 被调用是因为我在里面放了一个 'alert'。
存在异常 returned:'Exception: [object Object]'。 我不知道我没有设置什么导致这个异常。
谢谢。
这是只有新手才会错过的东西... 添加 [System.Web.Services.WebMethod] 方法之前的属性,GetMaxMgsLength。