我的Javascript函数中的WebMethod调用不会被PageMethod调用。下面是代码:
编辑控制台上写着:
Uncaught ReferenceError: PageMethods is not defined
JS:
function profilefollowbuttonchange(cn) {
if (cn.className == "profile-page-owner-follow-button") {
cn.className = "profile-page-owner-follow-button-active";
alert("camefollow");
PageMethods.ToggleFollow("follow", onSuccess, onFailure); //Does not trigger
alert("camefollow"); //Doesn't get printed
}
else {
cn.className = "profile-page-owner-follow-button";
alert("cameunfollow");
PageMethods.ToggleFollow("unfollow", onSuccess, onFailure); //Does not trigger
alert("cameunfollow"); //Doesn't get printed
}
}
function onSuccess() {
}
function onFailure() {
}
C#:
[WebMethod]
public static void ToggleFollow(string command)
{
//Does not reach this point.
}
是的,我在EnablePageMethods="true"标记中添加了ScriptManager标记。
但是,我在同一个页面中使用了两个WebMethods,用于两个不同的目的(两个不同的名称)。这会是问题吗?我不这么认为,但你们怎么想?
发布于 2015-06-01 05:51:12
看起来问题就在于脚本和ScriptManager
的执行顺序。这意味着,为了确保Javascript代码能够识别PageMethods
,您需要先加载ScriptManager
,然后启动Javascript函数。所以在我的逻辑中,这里需要一个简单的改变。您需要在脚本中使用$(document).ready()
,以确保ScriptManager
首先进入DOM,然后触发脚本。像这样的东西在这里会有帮助的。
$(document).ready(function () {
function profilefollowbuttonchange(cn) {
if (cn.className == "profile-page-owner-follow-button") {
cn.className = "profile-page-owner-follow-button-active";
alert("camefollow");
PageMethods.ToggleFollow("follow", onSuccess, onFailure); //Does not trigger
alert("camefollow"); //Doesn't get printed
}
else {
cn.className = "profile-page-owner-follow-button";
alert("cameunfollow");
PageMethods.ToggleFollow("unfollow", onSuccess, onFailure); //Does not trigger
alert("cameunfollow"); //Doesn't get printed
}
}
function onSuccess() {
}
function onFailure() {
}
});
只需用$(document).ready()
包装脚本代码,然后尝试它。
希望这能有所帮助。
发布于 2015-06-01 05:28:09
您的页面上配置了ScriptManager吗?
<asp:ScriptManager ID="scriptManager" runat="server" EnablePageMethods="true">
<Scripts>
<asp:ScriptReference Path="script1.js" />
</Scripts>
</asp:ScriptManager>
发布于 2021-04-23 12:31:41
如果出现持续时间过长的问题,只需再次检查javascript (我在那里有PageMehods而不是PageMethods )的调用,周五下午的编码盲区是:-)。
https://stackoverflow.com/questions/30566015
复制相似问题