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

时间:2015-06-01 05:14:06

标签: javascript c# asp.net webmethod pagemethods

我的WebMethod不会被我的Javascript函数中的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. 
}

是的,我在ScriptManager标记中添加了 EnablePageMethods =" true" 标记。

但是,我在同一页面中使用了两个WebMethods,用于两个不同的目的(两个不同的名称)。这可能是问题吗?我几乎不这么认为,但是你会怎么想?

3 个答案:

答案 0 :(得分:0)

您是否在页面上配置了ScriptManager?

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

答案 1 :(得分:0)

看起来问题在于脚本的执行顺序和PageMethods。这意味着要确保Javascript代码能够识别ScriptManager,您需要先加载$(document).ready(),然后启动该Javascript函数。所以在我的逻辑中,这里需要进行简单的更改。您需要在脚本中使用ScriptManager以确保$(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() { } }); 首先进入DOM,然后触发您的脚本。这样的事情应该有帮助。

$(document).ready()

使用<div ng-repeat="accObj in accountsArr" width="100%"> <div class="col" width="100%"> <div ng-if="$even" style="width:50%;height:70px;background-color:red"> {{accObj.account}}<br> {{accObj.type}} </div> <div ng-if="$odd" style="width:50%;height:70px;background-color:yellow"> {{accObj.account}}<br> {{accObj.type}} </div> </div> </div> 包装脚本代码,然后尝试。

希望这有帮助。

答案 2 :(得分:0)

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