前后端分离(一)

前后端分离(一)


一、基础知识

1、Ajax是Asynchronous JavaScript and XML 的缩写,即异步的JavaScript和XML。传统的网页(不使用Ajax)如果需要更新内容,必须重载整个网页。Ajax是一种无需重新加载整个网页的情况下,能够更新部分网页的技术。

2、一般处理程序:ASP.NET网站中这种扩展名为ashx的程序称为"一般处理程序",它与普通的扩展名为aspx的窗体网页程序不同的是没有HTML的窗体网页代码,适合作为后台服务器处理数据。

二、Ajax-DEMO

client.html

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5.     <title>Ajax</title>  
  6.     <script language="javascript">  
  7.     var http=null;  
  8.     function getHttp() {  
  9.         //获取XMLHttpRequest对象  
  10.         var http = null;  
  11.         try {  
  12.             if (window.ActiveXObject)  
  13.                 http = new ActiveXObject("Microsoft.XMLHTTP");  
  14.             else if (window.XMLHttpRequest)  
  15.                 http = new XMLHttpRequest();  
  16.             else  
  17.                 alert('Getting XMLHttpRequest faild');  
  18.         }  
  19.         catch (exp) {  
  20.             alert(exp.description);  
  21.         }  
  22.         return http;  
  23.     }  
  24.       
  25.     function process()  
  26.     {  
  27.         if(http.readyState==4)  
  28.         {  
  29.             //获取服务器响应,显示结果  
  30.             document.getElementById("msg").innerHTML = http.responseText;  
  31.         }  
  32.     }  
  33.   
  34.     function getDateTime(){  
  35.         try {  
  36.             //获取XMLHttpRequest对象  
  37.             if(http==null)  
  38.             {  
  39.                 http = getHttp();  
  40.             }  
  41.               
  42.             http.onreadystatechange=process;  
  43.               
  44.             //发送数据  
  45.             http.open("GET", "server.ashx?&rnd="+Math.random().toString(), true);  
  46.             http.send(null);  
  47.               
  48.         }  
  49.         catch (exp) {  
  50.             alert(exp.description);  
  51.         }  
  52.     }  
  53.     </script>  
  54.   
  55. </head>  
  56. <body>  
  57.     <img src="image.jpg" eidth="50" height="50"/>  
  58.     <input type="button" value="获取" onclick="getDateTime()" />  
  59.     <span id="msg" />  
  60. </body>  
  61. </html>  

1、加载同目录下的一张图片

2、每次点击“获取”按钮会像服务器端发送异步请求


server.ashx

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <%@ WebHandler Language="C#" Class="DEMO.server" Debug="true"%>  
  2. using System;  
  3. using System.Web;  
  4. using System.Threading;  
  5.   
  6. namespace DEMO  
  7. {  
  8.     public class server : IHttpHandler  
  9.     {  
  10.         public bool IsReusable  
  11.         {  
  12.             get  
  13.             {  
  14.                 return false;  
  15.             }  
  16.         }  
  17.         public void ProcessRequest(HttpContext context)  
  18.         {  
  19.         String s = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");  
  20.         Thread.Sleep(1000);  
  21.         //输出结果  
  22.             context.Response.Clear();  
  23.             context.Response.ContentType = "text/plain";  
  24.             context.Response.Write(s);  
  25.             context.Response.Flush();  
  26.         }    
  27.          
  28.     }  
  29. }  


当有请求来的时候、获取当前时间然后传回客户端(更复杂的功能可以以此为基础进行扩展)

Web.config

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <configuration>  
  3.   <system.web>  
  4.     <globalization requestEncoding="gb2312" responseEncoding="gb2312"/>  
  5.   </system.web>  
  6. </configuration>  


来源:
http://blog.youkuaiyun.com/finish_dream/article/details/51346037
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值