Javascript 在ASPX页中未定义PageMethods
我正在看一些旧代码,我只能假设它们在同一时间工作过 MyPage.aspx:Javascript 在ASPX页中未定义PageMethods,javascript,asp.net,ajax,pagemethods,Javascript,Asp.net,Ajax,Pagemethods,我正在看一些旧代码,我只能假设它们在同一时间工作过 MyPage.aspx: function GetCompanyList(officeId) { var companyList = document.getElementById('<%= CompanyDropDown.ClientID %>'); if (companyList.length == 0) PageMethods.GetCompanyList(officeId, OnGetComp
function GetCompanyList(officeId) {
var companyList = document.getElementById('<%= CompanyDropDown.ClientID %>');
if (companyList.length == 0)
PageMethods.GetCompanyList(officeId, OnGetCompanyList);
else
EditCompany();
}
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public IEnumerable<CompanyMinimum> GetCompanyList(int officeId) {
return (
from c in Repository.Query<Company>()
where !c.IsDeleted && c.TypeEnumIndex == (short)CompanyRelationshipType.Hotel
select new CompanyMinimum() {
id = c.Id,
desc = c.Description
}
).ToList();
}
函数GetCompanyList(officeId){
var companyList=document.getElementById(“”);
if(companyList.length==0)
GetCompanyList(officeId,OnGetCompanyList);
其他的
编辑公司();
}
和:
function GetCompanyList(officeId) {
var companyList = document.getElementById('<%= CompanyDropDown.ClientID %>');
if (companyList.length == 0)
PageMethods.GetCompanyList(officeId, OnGetCompanyList);
else
EditCompany();
}
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public IEnumerable<CompanyMinimum> GetCompanyList(int officeId) {
return (
from c in Repository.Query<Company>()
where !c.IsDeleted && c.TypeEnumIndex == (short)CompanyRelationshipType.Hotel
select new CompanyMinimum() {
id = c.Id,
desc = c.Description
}
).ToList();
}
代码隐藏:
function GetCompanyList(officeId) {
var companyList = document.getElementById('<%= CompanyDropDown.ClientID %>');
if (companyList.length == 0)
PageMethods.GetCompanyList(officeId, OnGetCompanyList);
else
EditCompany();
}
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public IEnumerable<CompanyMinimum> GetCompanyList(int officeId) {
return (
from c in Repository.Query<Company>()
where !c.IsDeleted && c.TypeEnumIndex == (short)CompanyRelationshipType.Hotel
select new CompanyMinimum() {
id = c.Id,
desc = c.Description
}
).ToList();
}
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
公共IEnumerable GetCompanyList(intOfficeId){
返回(
从Repository.Query()中的c
其中!c.IsDeleted&&c.TypeEnumIndex==(短)CompanyRelationshipType.Hotel
选择新公司最小值(){
id=c.id,
描述=c.描述
}
).ToList();
}
但是在调用第一个代码片段中的PageMethods.GetCompanyList()
时,Chrome报告:
未定义PageMethods
有人能看到是什么改变阻止了这一点吗
注意:我发现了类似的问题,但它们似乎都与此代码在母版页或用户控件中不起作用有关,这里不是这样。您可以通过jQuery调用ASP.NET AJAX页面方法,如下所示:
$.ajax({
type: "POST",
url: "PageName.aspx/MethodName",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Do something interesting here.
}
});
实际上只与页面
子类的方法交互,这些子类是公共
,静态
,并作为WebMethod
属性化
GetCompanyList
有两个,而且还需要是static
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public static IEnumerable<CompanyMinimum> GetCompanyList(int officeId) {
// ...
}
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
公共静态IEnumerable GetCompanyList(intOfficeId){
// ...
}
而且,我怀疑正在发生的事情是,如果它找不到所有3个方法的话,它会留下未定义的
PageMethods
客户端。也许你正在页面中使用路由。则必须在调用PageMethods后设置实路径:
PageMethods.set_path("<%=ResolveUrl("~/YourPage.aspx")%>");
PageMethods.YourMethod(param, OnSuccess, OnError);
PageMethods.set_路径(“”);
PageMethods.YourMethod(参数、OnSuccess、OnError);
我认为另一个解决方案应该给出的一个答案是,如果此错误发生在您的服务器上,而不是本地,请放置空的MyPage.aspx占位符文件,现在它也可以在生产服务器上工作。抱歉,我在第二个代码片段中粘贴了错误的代码。是,EnablePageMethods
设置为true。我熟悉使用MVC时使用的$.ajax()
,但是如果不重新编写代码也能正常工作,那就太好了。无论您尝试哪种版本的Chrome,以及ASP.NET ajax生成的JavaScript都可能会有问题,jQuery将为您提供更好的跨浏览器体验,但我理解你想让它工作的愿望。它在IE和/或Firefox中工作吗?在IE上的结果相同。看起来它可能在旧版本的Firefox上工作。在您将方法更改为静态之前,如果您使用jQuery的.ajax()
函数,它是否会给您带来相同的错误?您完全正确地认为该方法必须是静态的,但我不确定如果没有有效的页面方法,它会抱怨JavaScript代理未定义。@KarlAnderson:现在正试图确认。是的,这似乎是问题所在。不久前,当我们重构部分代码时,我发现它从静态变为非静态。虽然我还没有一个静态版本可以工作,但我现在得到的错误与我之前的错误不同。谢谢