jQuery ajax调用asp.cs文件里的方法

1.首先在 aspx.cs文件里建一个公开的静态方法,然后加上WebMethod属性(要引入System.Web.Services)。 cs页面: [WebMethod]//如果要在这个方法里操作Session,需要将WebMethod的EnableSession属性设为true.即 [WebMethod(EnableSession=true)]或[WebMethod(true)] public static string SayHello() { return "hello"; }

aspx页面:

<script type="text/javascript">
$.ajax({
type:"POST",
contentType:"application/json",
url:"Default.aspx/SayHello",
data:"{}",
dataType:"json",
success:function(result){
alert(result.d);//json里面的格式
}
});
</script>

运行结果:弹出消息框,显示hello.

posted @ 2012-06-11 22:56  loongso  阅读(1172)  评论(0编辑  收藏  举报