惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google Online Security Blog
Google Online Security Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
Cisco Talos Blog
Cisco Talos Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Scott Helme
Scott Helme
Project Zero
Project Zero
E
Exploit-DB.com RSS Feed
S
Secure Thoughts
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
NISL@THU
NISL@THU
WordPress大学
WordPress大学
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 热门话题
小众软件
小众软件
P
Privacy & Cybersecurity Law Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
About on SuperTechFans
Hacker News: Ask HN
Hacker News: Ask HN
AWS News Blog
AWS News Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hacker News: Front Page
F
Full Disclosure
Latest news
Latest news
Schneier on Security
Schneier on Security
The Hacker News
The Hacker News
T
Troy Hunt's Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Jina AI
Jina AI
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
G
GRAHAM CLULEY
Forbes - Security
Forbes - Security
V
V2EX - 技术
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Vulnerabilities – Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
MongoDB | Blog
MongoDB | Blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
P
Privacy International News Feed
C
Check Point Blog
N
News and Events Feed by Topic

博客园 - Gerald1983

[转载]AJAX 框架 用 Asp.net ajax 还是 Jquery ? [转]触发器 Excel导出报错 Delete File - Gerald1983 - 博客园 SQLServer Service Can't Start - Gerald1983 DataSource of GridView is Excel - Gerald1983 SQL - Using CASE in a JOIN SQL SERVER 与ACCESS、EXCEL的导入导出(转载) Asp.net页面的生命周期 Asp.net页面的生命周期 DataGrid /GridView分页(sqlserver/oracle)-----转载 sql语句中包括单引号和双引号的问题 XmlDocument操作xml文档 (转) 一个Ajax实例(成本项目) Oracle存储过程编写经验和优化措施(转) GridView的美化 - Gerald1983 数据绑定时的前台页面上的逻辑判断 (转) 如何去掉DataTable中的重复行(新增.net 2.0中最新解决方法---简便) (转) C#代码与javaScript函数的相互调用(转)
Ajax实例转载
Gerald1983 · 2008-06-26 · via 博客园 - Gerald1983

一、XMLHttpRequest 对象的方法与属性

   

   

abort()

停止当前请求

getAllResponseHeaders()

HTTP请求的所有响应首部作为键/值对返回

getResponseHeader("header")

返回指定首部的串值

open("method", "url")

建立对服务器的调用。method参数可以是GETPOSTPUTurl参数可以是相对URL或绝对URL。这个方法还包括3个可选的参数

send(content)

向服务器发送请求

setRequestHeader("header", "value")

把指定首部设置为所提供的值。在设置任何首部之前必须先调用open()


  属 

   

onreadystatechange

每个状态改变时都会触发这个事件处理器,通常会调用一个JavaScript函数

readyState

请求的状态。有5个可取值:0 = 未初始化,1 = 正在加载,2 = 已加载,3 = 交互中,4 = 完成

responseText

服务器的响应,表示为一个串

responseXML

服务器的响应,表示为XML。这个对象可以解析为一个DOM对象

status

服务器的HTTP状态码(200对应OK404对应Not Found(未找到),等等)

statusText

HTTP状态码的相应文本(OKNot Found(未找到)等等)

二、使用XMLHttpRequest 实现ajax效果
这里我们用客户端通过XMLHttpRequest 请求服务端获取当前系统时间,实现异步交互!
客户端myAjax.html代码

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    
<title>xmlhttprequest ajax demo</title>
    
<script type ="text/javascript" language ="javascript" >
        
var req; //定义变量,用来创建xmlhttprequest对象
        function creatReq() // 创建xmlhttprequest,ajax开始
        {
            
var url="ajaxServer.aspx"//要请求的服务端地址
            if(window.XMLHttpRequest) //非IE浏览器,用xmlhttprequest对象创建
            {
                req
=new XMLHttpRequest();
            }

            
else if(window.ActiveXObject) //IE浏览器用activexobject对象创建
            {
                req
=new ActiveXObject("Microsoft.XMLHttp");
            }

            
            
if(req) //成功创建xmlhttprequest
            {
                req.open(
"GET",url,true); //与服务端建立连接(请求方式post或get,地址,true表示异步)
                req.onreadystatechange = callback; //指定回调函数
                req.send(null); //发送请求
            }

        }

        
        
function callback() //回调函数,对服务端的响应处理,监视response状态
        {
            
if(req.readystate==4//请求状态为4表示成功
            {
                
if(req.status==200//http状态200表示OK
                {
                    Dispaly(); 
//所有状态成功,执行此函数,显示数据
                }

                
else //http返回状态失败
                {
                    alert(
"服务端返回状态" + req.statusText);
                }

            }

            
else //请求状态还没有成功,页面等待
            {
                document .getElementById (
"myTime").innerHTML ="数据加载中";
            }

        }

        
        
function Dispaly() //接受服务端返回的数据,对其进行显示
        {
            document .getElementById (
"myTime").innerHTML =req.responseText;
        }

        
    
</script>
</head>
<body>
    
<div id="myTime"></div>
        
    
<input id="Button1" type="button" value="Get Time"  onclick ="creatReq();"/>
</body>
</html>

服务端ajaxServer.aspx代码

public partial class ajaxServer : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        System.Threading.Thread.Sleep(
1000); //为了看到ajax效果,将当前线程延时1000毫秒
        Response.Write(DateTime .Now .ToString ()); //输出当前时间
    }

}