学习ajax 笔记(一)

学习ajax 笔记(一)

1.传统页面的过程

很多文件都是通过http协议请求来传送的

2.Ajax应用    

 异步的发生请求

页面部分刷新

减少数据传输量

(请求的数据传输量不变,主要是回发的数据传输量)

提高用户的体验

   (胡乱应用的话,会造成反方向的结果)

3.   asp.net的ajax应用 

服务器为中心的开发(是不用写js代码的)

客户端为中心开发(提供了丰富的支持)

4.microsoft Ajax library

 javascript基础扩展

 浏览器兼容层(ie,forbox)

 面向对象类型系统(维护和扩展的方法)

 提供一个异步通信层(对象进行封装进行扩张,服务器端和客户端之间的通信

 提供客户端基础类库(一些模型的基础)

下面是一段用JavaScript实现的面向对象的系统

客户端相应用户请求


<script language="javascript" type="text/javascript">
    
//注册一个命名空间(一个类面向对象)
     Type.registerNamespace("AspNetAjaxOverView");
     
//定义一个Person类
     //顶一个构造函数,有两个参数,将用户传入的两个参数保存下来
     //加下划线的是私有成员
     AspNetAjaxOverView.Person=function (firstName,lastName)
     {
         
this._firstName=firstName;
         
this._lastName=lastName ;
     }
     
//定义成员方法
     AspNetAjaxOverView.Person.prototype =
     {
        
//得到其属性(方法)
        get_firstName :function()
        {
           
return this._firstName;
        },
        get_lastName :
function()
        {
          
return this._lastName;
        },
        toString :
function()
        {
           
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>

 以及一个用异步通信层实现信息传输


 <script language="javascript" type="text/javascript">
    
function showEmployee(firstName, lastName, title)
    {
        
//异步通信层定义一些类
        var request=new Sys.Net.WebRequest();
        
//(2)设置请求URL
        request.set_url("GetEmployee.ashx");
         
//(3)设置请求方式
        request.set_httpVerb("POST");
        
//(4)设置请求完成时的处理函数
        //回调函数含义:发出一个请求,服务器通过你指定一个回调函数来回复你
        request.add_completed(onGetEmployeeComplete);
        
        
//encodeURIComponent进行转义
        var requestBody=String.format(
        
"firstName={0}&lastName={1}&title={2}",
        encodeURIComponent(firstName),
        encodeURIComponent(lastName),
        encodeURIComponent(title));
        
//传进去
        request.set_body(requestBody);
        
        
//发送信息
        request.invoke();
        
        
function onGetEmployeeComplete(response)
        {
            
if(response.get_responseAvailable())
            {
              
var employee = response.get_object();
              alert(String.format(
                        
"Hello I'm {0} {1}, my title is '{2}'",
                        employee.FirstName,
                        employee.LastName,
                        employee.Title));
            }
        }
    }
    
</script>

对应请求在一般处理程序写  


<%@ WebHandler Language="C#" Class="AspNetAjaxOverview.GetEmployee" %>

using System;
using System.Web;
using System.Web.Script.Serialization;

namespace AspNetAjaxOverview
{
    
public class GetEmployee : IHttpHandler
    {
        
//让服务器端相应客户端的请求,并发出相应的信息
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType 
= "text/plain";
            
            
string firstName = context.Request.Params["firstName"];
            
string lastName = context.Request.Params["lastName"];
            
string title = context.Request.Params["title"];
            Employee employee 
= new Employee(firstName, lastName, title);

            
//将对象序列化为json数据格式
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            
string jsonEmp = serializer.Serialize(employee);
            
            
//进行输出
            context.Response.Write(jsonEmp);
        }

        
public bool IsReusable
        {
            
get
            {
                
return false;
            }
        }

    }
}

其中的Employee类是


public class Employee
    {
        
private string _FirstName;
        
private string _LastName;
        
private string _Title;

        
public Employee() { }

        
public Employee(string firstName, string lastName, string title)
        {
            
this._FirstName = firstName;
            
this._LastName = lastName;
            
this._Title = title;
        }

        
public string FirstName
        {
            
get
            {
                
return this._FirstName;
            }
        }

        
public string LastName
        {
            
get
            {
                
return this._LastName;
            }
        }

        
public string Title
        {
            
get
            {
                
return this._Title;
            }
        }
    }

客户端访问WebService方法

对应的JavaScript


<script language="javascript" type="text/javascript">
            function showEmployee(firstName, lastName, title)
            {
                
//调用WebService的方法
                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>

webservice代码


<%@ WebService Language="C#" Class="AspNetAjaxOverview.EmployeeService" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

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);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于学习Ajax,我可以为您提供一些学习笔记。以下是一份简要的Ajax学习笔记,希望对您有所帮助: 1. 什么是Ajax? - Ajax全称为Asynchronous JavaScript and XML(异步JavaScript和XML),是一种用于创建交互式Web应用程序的技术。 - 它通过在后台与服务器进行数据交换,实现在不刷新整个页面的情况下更新部分页面内容。 2. Ajax的优点: - 异步处理:可以在后台发送和接收数据,而无需刷新整个页面。 - 提高用户体验:通过部分更新页面内容,可以提供更快的响应时间和更流畅的用户体验。 - 减轻服务器负担:只更新需要的部分内容,减少了不必要的数据传输和服务器负载。 3. Ajax的基本原理: - 使用XMLHttpRequest对象向服务器发送请求,并接收响应。 - 通过JavaScript在前端处理响应数据。 - 更新页面内容,以显示最新的数据。 4. Ajax的核心技术: - XMLHttpRequest对象:用于与服务器进行数据交换。 - JavaScript:用于处理响应数据和更新页面内容。 - XML或JSON:用于传输数据格式。 5. Ajax的使用步骤: - 创建XMLHttpRequest对象。 - 定义请求类型、URL和是否异步。 - 发送请求并接收响应。 - 处理响应数据并更新页面内容。 6. 常见的Ajax框架和库: - jQuery:一个流行的JavaScript库,提供了简化Ajax开发的方法和函数。 - Axios:一个基于Promise的HTTP客户端,用于浏览器和Node.js。 - Fetch API:一种用于发送和接收网络请求的新标准。 这只是Ajax学习的一些基本概念和步骤,您可以进一步深入学习Ajax的相关知识和实践。希望这些笔记对您有所帮助!如有更多问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值