第一步:
后台要调用的方法
[System.Web.Services.WebMethod]
public static string [] TestAjax( string paraments)
{
string [] a = { " 1 " , " 2 " };
return a;
}
public static string [] TestAjax( string paraments)
{
string [] a = { " 1 " , " 2 " };
return a;
}
第二步:
js调用后台方法
代码
<
script type
=
"
text/javascript
"
>
function test() {
PageMethods.TestAjax(paraments , funSuccess, funError);
}
function funSuccess(result) {
alert(result[ 0 ]);
}
function funError(err) {
alert( " error " + error._message);
}
< / script>
function test() {
PageMethods.TestAjax(paraments , funSuccess, funError);
}
function funSuccess(result) {
alert(result[ 0 ]);
}
function funError(err) {
alert( " error " + error._message);
}
< / script>
第三步:
页面添加
<
asp:ScriptManager ID
=
"
ScriptManager1
"
runat
=
"
server
"
EnablePageMethods
=
"
true
"
>
</ asp:ScriptManager >
</ asp:ScriptManager >
如果出现
"PageMethods未定义"或"对象不支持此属性或方法"解决方法
1.检查web.config中是否加入对于asp.net ajax的支持的代码
2.检查ScriptManager中是否设置了EnableMethods=true
3.检查后台cs中是否引用的命名空间System.Web.Services或者加入了[System.Web.Services.WebMethod]
4.后台函数必须是public static
5. 还有一种情况就是,通常,有些人在复制这个aspx页面时,经常是连同,<%@ Page Language="C#" AutoEventWireup="true" CodeFile="addSight.aspx.cs" Inherits="Page_message_addSight" %>一起复制了,所以造成文件头的映射出现错误,导致PageMethods的方法指向出现错误,而这种错误并没有显示那里错误,所以检查这样的错误。这是新手经常出现的错误。