教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 c#中js调用后台的方法介绍

c#中js调用后台的方法介绍

发布时间:2016-04-07   编辑:jiaochengji.com
本文介绍下,js调用C#后台代码的方法,有需要的朋友,可以参考学习下。

本节内容,需要三步来完成。

步骤1,后台要调用的方法
 

复制代码 代码示例:
[System.Web.Services.WebMethod]
public static string[] TestAjax(string paraments)
{
string[] a = { "1", "2" };
return a;
}

步骤2,js调用后台的方法
 

复制代码 代码示例:

<script type="text/javascript">
/**
* 调用后台的方法
* edit www.jbxue.com
*/
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>

可能的问题,如果出现:"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的方法指向出现错误,而这种错误并没有显示那里错误,所以检查这样的错误。
这个也是新手经常出现的错误。

您可能感兴趣的文章:
c#中js调用后台的方法介绍
C#与js前后台互相调用的实现代码
js调用后台c#代码的几种方法
Asp.net后台调用js脚本的方法
asp.net中JS,CS 调用后台变量的值多种方法
javascript读取c#数据方法详解
jquery dialog open后,服务器端控件失效的快速解决方法
js去掉空格的代码
smarty无法调用js脚本的解决方法
jquery.Ajax()方法调用Asp.Net后台的方法解析

[关闭]
~ ~