加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

前台访问后台的几种方法

发布时间:2020-12-16 23:57:37 所属栏目:安全 来源:网络整理
导读:1、WebMethod public static方法,ajax/PageMethods调用。 后台方法,test.cs [WebMethod] public static string GetUserName() { //...... }需要访问操作session时[WebMethod(EnableSession = true)]//或[WebMethod(true)] public static string GetUserNam

1、WebMethod

public static方法,ajax/PageMethods调用。

后台方法,test.cs

[WebMethod]
 public static string GetUserName() 
 {
 //......
 }
需要访问操作session时
[WebMethod(EnableSession = true)]//或[WebMethod(true)]
 public static string GetUserName() 
 {
 //......
 }

前台js直接调用,test.aspx

PageMethods.GetUserName

前台ajax调用,test.js

$.ajax({
        type: "POST",contentType: "application/json",url: "WebForm2.aspx/GetUserName",data: "{}",dataType: "json",success: function(){.......}
    });

参数说明。
  type:请求的类型,这里必须用post。(WebMethod方法只接受post类型的请求)
  contentType:发送信息至服务器时内容编码类型,这里一定要用application/json
  url:请求的服务器端处理程序的路径,格式为"文件名(含后缀)/方法名"
  data:参数列表。
????????? 注意,这里的参数一定要是json格式的字符串,记住是字符串格式,如:"{aa:11,bb:22,cc:33,...}";
????????? 如果你写的不是字符串,那jquery会把它实序列化成字符串,那么在服务器端接受到的就不是json格式了;
????????? 不能为空,即使没有参数也要写成"{}",如上例。很多人不成功,原因就在这里。
  dataType:服务器返回的数据类型。必须是json,其他的都无效。因为webservice 是一json格式返回数据的,其形式为:{"d":"......."}。
  success:请求成功后的回调函数。你可以在这里对返回的数据做任意处理。

上面ajax的简单封装,jquery.extend.js

///	<summary>
///   ? jQuery原型扩展,重新封装Ajax请求WebServeice
///	</summary>
///	<param name="url" type="String">
///     处理请求的地址
///	</param>
///	<param name="dataMap" type="String">
///     参数,json格式的字符串
///	</param>
///	<param name="fnSuccess" type="function">
///     请求成功后的回调函数
///	</param>
$.ajaxWebService = function(url,dataMap,fnSuccess) {
    $.ajax({
        type: "POST",url: url,data: dataMap,success: fnSuccess
    });
}

请求方法

$.ajaxWebService("WebForm2.aspx/GetUserName","{}",function(result) {......});


2、一般处理程序(*.ashx)

处理速度比aspx快,是专为ajax服务。
接受参数用context.Request[" "];
编写多个方法使用Switch语句;
返回值用context.Response.Write( );

前台调用

DefaultHandler.ashx?method=getlist

后台CS

  using System;
  using System.Web;
  using System.Collections.Generic; 
  using System.Web.Script.Serialization;
   
  public class DefaultHandler  IHttpHandler { 
  public void ProcessRequest (HttpContext context) 
  {
  string response = string.Empty;
  string str = context.Request.QueryString["method"]; 
   
  if (string.IsNullOrEmpty(str)) 
  { 
  context.Response.Write("error!"); 
  return; 
  } 
   
  switch (str) 
  { 
  case "getlist" 
  response = GetList(context); 
  break; 
  //下边还有可以接着写. 
  } 
  context.Response.Write(response);
  context.Response.End();
   
  } 
   
  public bool IsReusable { 
  get { 
  return false; 
  } 
  } 
   
  public string GetList(HttpContext context) 
  {
   
  //从数据库取得list数据: 
  List<myEntity> data = DAL.GetData();
   
  return ToJson(data.ToArray());
   
  } 
   
  //序列化对象为json数据 
  public string ToJson(object o) 
  { 
  JavaScriptSerializer j = new JavaScriptSerializer(); 
  return j.Serialize(o); 
  }
  
  } 
   


3、web服务(*.asmx)

必须是json格式、跨浏览器、跨平台。

??
? 4、直接嵌入
前台html或者js里直接嵌入后台的public方法/属性
<%=abc()%>
 $(document).ready(function() {  
 sshow();  
 });  
   
 function sshow()  
 {    
      var s = '<%=IsShow() %>';    
      if(s == '0')  
      {  
       document.getElementById("trr").style.display= "none";       
      }else  
      {  
      document.getElementById("trr").style.display= "";   
      }  
      alert(s);  
 }  

public int IsShow()  
   {  
       int sis = 0;  
       ASPxLabel urll = (ASPxLabel)DataList1.Items[0].FindControl("ASPxLabelURL");  
       //Response.Write(urll.Text.Length);  
       //Response.Write(urll.Text.IndexOf(".",9,2).ToString());  
       if (urll.Text.Length > 12)  
?      {  
           if (urll.Text.IndexOf(".",3) > -1)  
           {  
               sis = 1;  
           }  
       }  
       return sis;  
   } 
??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读