zoukankan      html  css  js  c++  java
  • ajax学习笔记(1)

                var prm = Sys.WebForms.PageRequestManager.getInstance();
                alert(prm.get_isInAsyncPostBack()); //判断是否为异步提交

    一.Ajax类

    <form id="form1" runat="server">
            
    <asp:ScriptManager ID="ScriptManager1" runat="server">
            
    </asp:ScriptManager>
        
        
    <script language="javascript" type="text/javascript">
            Type.registerNamespace(
    "AspNetAjaxOverView"); //注册命名空间
            //写一个类,=号后为构造方法
            AspNetAjaxOverView.Person = function(firstName, lastName)
            
    {
                
    this._firstName = firstName;
                
    this._lastName = lastName;
            }

        
    //写个属性,get方法以get_开头,set方法同上    AspNetAjaxOverView.Person.prototype = 
            {
                get_firstName : 
    function()
                
    {
                    
    return this._firstName;
                }
    ,
                get_lastName : 
    function()
                
    {
                    
    return this._lastName;
                }
    ,
                toString : 
    function()
                
    {//toString覆盖了类的toString方法
                    return String.format("Hello, I'm {0} {1}.",
                        
    this.get_firstName(),
                        
    this.get_lastName());
                }

            }

        
    //把这个类注册到命名空间
    AspNetAjaxOverView.Person.registerClass("AspNetAjaxOverView.Person");

        //定义子类    
            AspNetAjaxOverView.Employee = function(firstName, lastName, title)
            
    {
        
    //继承父类的构造方法
    AspNetAjaxOverView.Employee.initializeBase(this, [firstName, lastName]);

                
                
    this._title = title;
            }

            AspNetAjaxOverView.Employee.prototype 
    = 
            
    {
                get_title : 
    function()
                
    {
                    
    return this._title;
                }
    ,
                toString : 
    function()
                
    {
    //调用父类中的toString方法
                    return AspNetAjaxOverView.Employee.callBaseMethod(this"toString"+ 
                        
    " My title is '" + this.get_title() + "'.";
                }

            }

        
    //注册时第二个参数表明为继承父类
        AspNetAjaxOverView.Employee.registerClass("AspNetAjaxOverView.Employee", AspNetAjaxOverView.Person);

        </script>
    二.异步通讯类
    1.服务器端定义一个C#类
      public class Employee
     {
      private string _FirstName;
      private string _LastName;
      private string _Title;

      public Employee() { }
      public Employee(string firstName, string lastName, string title)
     
    public string FirstName
      public string LastName
      public string Title
     }
    2.服务器端接收请求的GetEmployee.ashx

    GetEmployee.ashx

    3.客户端调用这个类

    Code

    三.客户端调用Webservice
    1.服务器端Webservice
    namespace AspNetAjaxOverview
    {
        [WebService(Namespace 
    = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo 
    = WsiProfiles.BasicProfile1_1)]
        [ScriptService] 
    //允许客户端调用的类标记(必须)
        public class EmployeeService : System.Web.Services.WebService
        
    {
            [WebMethod]
            [ScriptMethod]  
    //允许客户端调用的方法标记(必须)
            public Employee GetEmployee(string firstName, string lastName, string title)
            
    {
                
    return new Employee(firstName, lastName, title);
            }

        }

    }
    2.客户端调用
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        
    <Services>
        
    <!--为ScriptManager指定WebService服务器端程序,会自动生成客户端代理-->
            
    <asp:ServiceReference Path="EmployeeService.asmx" />
        
    </Services>
    </asp:ScriptManager>

    <script language="javascript" type="text/javascript">
        
    function showEmployee(firstName, lastName, title)
        
    {//在这里就可以直接调用WebServic方法了
            AspNetAjaxOverview.EmployeeService.GetEmployee(
                firstName,
                lastName,
                title,
                onGetEmployeeSuccess);
        }

        
    //回调函数参数为服务器端类实例,通过代理可直接使用
        function onGetEmployeeSuccess(employee)
        
    {
            alert(String.format(
                
    "Hello I'm {0} {1}, my title is '{2}'",
                employee.FirstName,
                employee.LastName,
                employee.Title));
        }

    </script>
  • 相关阅读:
    Web框架本质及浅谈HTTP协议
    mysql
    jQuery
    Css
    html
    socket编程
    面向对象and类
    模块
    装饰器
    cef network-settings
  • 原文地址:https://www.cnblogs.com/gghxh/p/959654.html
Copyright © 2011-2022 走看看