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.