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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - sunfishlu

一步一步学习Windows Azure(二)Azure之Hello World 一步一步学习Windows Azure(一)概述 一步一步学习CakePHP(三)model - sunfishlu 一步一步学习CakePHP(二)controllers 一步一步学习CakePHP(一)基本概念 JQuery写的个性导航菜单 - sunfishlu JQueryUI(五):Dialog(第二部分) JQueryUI(四):Dialog(第一部分) - sunfishlu JQueryUI(三):Accordion JQueryUI(二):Tabs(第二部分) JQueryUI(一):Tabs(第一部分) - sunfishlu - 博客园 封装的Ext Grid 无法在Web服务器上启动调试。您不具备调试此应用程序的权限,此项目的URL位于Internet区域。 - sunfishlu - 博客园 ToolTip(图片文字) with Jquery - sunfishlu Ext中combobox在Grid里显示问题 javascript与dom编程(四)animation(例)Tooltips javascript与dom编程(三)animation - sunfishlu extjs grid设置某列背景颜色 javascript与dom编程(二)Event
javascript与dom编程(五)ajax
sunfishlu · 2009-06-01 · via 博客园 - sunfishlu

一:创建XMLHttpRequest对象

    在使用XMLHttpRequest对象发送请求和处理响应之前,必须先用JavaScript创建一个XMLHttpRequest对象,在IE56)中是把XMLHttpRequest实现为一个ActiveX对象,如:var requester = new ActiveXObject("Micrsoft.xmlHTTP");

其它的浏览器包括IE7是把实现为一个JavaScript对象。如:

var requester = new XMLHttpRequest();

    我们可以通过try-catch,在不同的浏览器中,正确的创建XMLHttpRequest对象。

try

{

    var requester = new XMLHttpRequest();

}

catch(error)

{

    try

    {

       var requester = new ActiveXObject("Microsoft.XMLHTTP");

    }

    catch(error)

    {

       var requester = null;

    }

}

二:与服务器通信

创建好XMLHttpRequest后,我们需要调用opensend两个方法,用来从服务器取得数据。

open用来初始化与服务器的连接,它带有两个必选的参数。第一个参数为请求的方式,(getpostput),第二个为获得数据的url地址。例如:

requester.open("GET", "/feed.xml", true);第三个参数指定请求是异步还是同步,默认为异步。

send方法激活此连接,向服务器发送请求。当使用POST请求时,send允许发送一个已经编码过的参数。

requester.setRequestHeader("Content-Type",

"application/x-www-form-urlencoded");//Opera浏览器下

requester.open("POST", "/query.php", true);

requester.send("name=Clark&email=superman@justiceleague.xmp");

使用GET时,参数必须随着url传送。例如:

requester.open("GET",

"query.php?name=Clark&email=superman@justiceleague.xmp", true);

requester.send(null);

IE下,send不需要任何参数,但是在firefox中则会出现错误。所以我们必须给send一个null的参数。

调用send后,XMLHttpRequest将会与服务器通信,返回你所请求的数据。为了确认服务器已经响应请求,我们可以监视XMLHttpRequestreadyState属性,有5个可取值,0=初始化,1=正在加载,2=已加载,3=交互中,4=完成。

请求完成后,我们还必须确认XMLHttpRequest是否正确得到数据,我们可以查看statusHTTP状态码,200对应OK404对应Not Found

三:处理数据

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

2responseText:服务器的响应,表示为一个串。