jquery PageMethod说该方法不存在

时间:2012-12-13 07:04:55

标签: 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'

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

1 个答案:

答案 0 :(得分:3)

而不是使用ScriptManager我想用jQuery调用page方法。请仔细阅读这篇好文章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.
   }
});