简体   繁体   English

PageMethods 未定义异常:不调用 ASP.NET 中的 WebMethod

[英]PageMethods is not defined Exception: Does not call the WebMethod in ASP.NET

My WebMethod does not get called by the PageMethod call in my Javascript function.我的 WebMethod 没有被我的 Javascript 函数中的 PageMethod 调用调用。 Here's the code:这是代码:

EDIT The Console says:编辑控制台说:

Uncaught ReferenceError: PageMethods is not defined

JS: 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#: C#:

[WebMethod]
public static void ToggleFollow(string command)
{
       //Does not reach this point. 
}

And yes I have added the EnablePageMethods="true" tag in the ScriptManager tag.是的,我在 ScriptManager 标签中添加了EnablePageMethods="true"标签。

However, I have used two WebMethods in the same page for two different purposes (Two different names).但是,为了两个不同的目的(两个不同的名称),我在同一页面中使用了两个 WebMethod。 Could that be the issue?这可能是问题吗? I hardly think so, but what do yall think?我几乎不这么认为,但是你们觉得呢?

Do you have ScriptManager configured on your page?您的页面上是否配置了 ScriptManager?

<asp:ScriptManager ID="scriptManager" runat="server" EnablePageMethods="true">
    <Scripts>
        <asp:ScriptReference Path="script1.js" />
    </Scripts>
</asp:ScriptManager>

It looks like the problem is with your execution sequence of the script and the ScriptManager .看起来问题出在脚本和ScriptManager执行顺序上。 This means that to make sure the PageMethods is recognized by the Javascript code, you need to load the ScriptManager first and then fire that Javascript function.这意味着要确保 JavaScript 代码识别PageMethods ,您需要先加载ScriptManager ,然后触发该 Javascript 函数。 So in my logic, a simple change is required here.所以在我的逻辑中,这里需要一个简单的改变。 You need to use $(document).ready() here in your script to make sure that ScriptManager first into the DOM and then your script is fired.您需要在脚本中使用$(document).ready()以确保ScriptManager首先进入 DOM,然后触发您的脚本。 Something like this should help here.这样的事情应该在这里有所帮助。

$(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() {
}
});

Just wrap your script code with $(document).ready() and then try it.只需用$(document).ready()包装您的脚本代码,然后尝试它。

Hope this helps.希望这可以帮助。

如果出现过长的问题,只需仔细检查来自 javascript 的调用,我在那里使用的是 PageMehods 而不是 PageMethods 并且我在周五下午编码失明:-)。

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

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