jquery PageMethod 说该方法不存在

标签 jquery asp.net ajax webmethod pagemethods

我正在尝试获取此 ASP.NET 函数的值:

[ScriptMethod, WebMethod]
public static bool checkRefresh()
{
    return isChanging;
}

通过使用 PageMethod 在 jquery 中调用它:

var isAllowed = false;
$("#edit").click(function () {
    PageMethods.editFunc();
    PageMethods.checkRefresh(DisplayMyResult);   //this line gives the error, though it's probably because it's first.
});

function DisplayMyResult(ResultString) {
    isAllowed = ResultString;
    if (isAllowed == true || isAllowed == "true") {
        location.reload();
    }
    else {
        PageMethods.checkRefresh(DisplayMyResult);
    }
}

我得到的错误(在 Chrome 控制台中)是

Uncaught TypeError: Object function () {
PageMethods.initializeBase(this);
this._timeout = 0;
this._userContext = null;
this._succeeded = null;
this._failed = null;
} has no method 'checkRefresh'

我不知道为什么它不起作用,有人可以帮忙吗?

最佳答案

我不想使用 ScriptManager,而是想使用 jQuery 调用页面方法。请阅读这篇好文章Using jQuery to directly call ASP.NET AJAX page methods

  [WebMethod]
  public static string checkRefresh()
  {
    return isChanging;
  }


$.ajax({
  type: "POST",
  url: "PageName.aspx/checkRefresh",
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Do something interesting here.
   }
});

关于jquery PageMethod 说该方法不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13854712/

相关文章:

c# - DesignOnly 属性不会在运行时隐藏属性

javascript - TinyMCE 不启动 ajax 加载的文本区域

javascript - 如何在 WordPress 中隐藏自定义控件?

jquery datatable(TableTool)自定义按钮调用事件

c# - Mac OS X Mono ASP.NET和C#编译过程

javascript - 在 ASP.NET 中离开页面之前保存数据

javascript - jQuery/Javascript : More complex Searching and Replacing in an HTML document

javascript - Mouse Out Orange Div "CurrentHeight"未应用

javascript - Html 表格行未附加到指定的表格

jquery - 选择2 : Trim entered text and if empty - prevent sending Ajax requests