smhy8187

 

HTTP Request

What is an HTTP Request?
什么是HTTP Request?

With an HTTP request, a web page can make a request to, and get a response from a web server - without reloading the page. The user will stay on the same page, and he or she will not notice that scripts might request pages, or send data to a server in the background.
有了HTTP请求,页面可以直接向服务器提请要求,或接受服务器回应,而不必再重新加载页面。使用者仍停留在原来页面上,而不会注意到暗地里脚本已经向服务器提出请求或传送数据了。

By using the XMLHttpRequest object, a web developer can change a page with data from the server after the page has loaded.
通过使用XMLHttpRequest 对象,网络开发者在页面下载之后,可以根据从服务器那里获取的数据来更改页面。

Google Suggest is using the XMLHttpRequest object to create a very dynamic web interface: When you start typing in Google's search box, a JavaScript sends the letters off to a server and the server returns a list of suggestions.
Google Suggest 是使用XMLHttpRequest对象来创建一个极具动态效果的网页分界页面:当你开始在Google的搜索框内打入字母时,一份JAVA脚本会将系列字母送给服务器,而服务器则返回一系列建议。


 

Is the XMLHttpRequest Object a W3C Standard?
XMLHttpRequest对象是W3C的使用标准吗?

The XMLHttpRequest object is not a W3C standard.
XMLHttpRequest对象不是W3C的使用标准

The W3C DOM Level 3 "Load and Save" specification contains some similar functionality, but these are not implemented in any browsers yet. So, at the moment, if you need to send an HTTP request from a browser, you will have to use the XMLHttpRequest object.
The W3C DOM Level 3的“下载和保存”(Load and Save)规格包含着一些相似的功能,但这些没有在任何浏览器中被执行。所以,现在,如果你想要从浏览器发一份HTTP要求,你必须得用XMLHttpRequest对象。


Creating an XMLHttpRequest Object
创建一个XMLHttpRequest对象

For Mozilla, Firefox, Safari, Opera, and Netscape:
为 Mozilla, Firefox, Safari, Opera,和Netscape创建一个XMLHttpRequest对象

var xmlhttp=new XMLHttpRequest()

For Internet Explorer:
为IE创建:

var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")

Example
例子

<script type="text/javascript">
var xmlhttp
function loadXMLDoc(url)
{
xmlhttp=null
// code for Mozilla, etc.
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest()
}
// code for IE
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change
xmlhttp.open("GET",url,true)
xmlhttp.send(null)
}
else
{
alert("Your browser does not support XMLHTTP.")
}
}
function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
{
// if "OK"
if (xmlhttp.status==200)
{
// ...some code here...
}
else
{
alert("Problem retrieving XML data")
}
}
}
</script>

Try it yourself using JavaScript
尝试用JavaScript创建

The syntax is a little bit different in VBScript: Try it yourself using VBScript
在语法上VBscript会有点不同 使用VBScript再试一次

Note: An important property in the example above is the onreadystatechange property. This property is an event handler which is triggered each time the state of the request changes. The states run from 0 (uninitialized) to 4 (complete). By having the function xmlhttpChange() check for the state changing, we can tell when the process is complete and continue only if it has been successful.
注意:
以上例子的重要特性是“随时可变状态(onreadystatechange)”特性。这个特性是“事件处理者”,每当请求状态改变时,它都会被触发。状态从0(初始化)到4(完成)。通过xmlhttpChange()功能对状态改变的检查,我们能够区分程序是否结束,或将继续。


 

Why are we Using Async in our Examples?
为何我们在例子里用到Async?

All the examples here use the async mode (the third parameter of open() set to true).
这儿所有例子都用了async模式(open()元素的第三个参数设置为“真”)

The async parameter specifies whether the request should be handled asynchronously or not. True means that script continues to run after the send() method, without waiting for a response from the server. false means that the script waits for a response before continuing script processing. By setting this parameter to false, you run the risk of having your script hang if there is a network or server problem, or if the request is long (the UI locks while the request is being made) a user may even see the "Not Responding" message. It is safer to send asynchronously and design your code around the onreadystatechange event!
Async 参数指定了请求是否被同时处理。“真(TRUE)”意味着在send()方法后脚本继续运行,不用等服务器作出回应。“假(FALSE)”意味着脚本继续运行前要等待回应。通过设置参数为假,如果网络或服务器有问题,你的脚本就可能被悬置,或者如果请求太长(发出请求时UI锁定),使用者可能会看到“没有回应”的信息。在“随时可变状态(onreadystatechange)”事件附近设计代码,不同时发出请求,这样会更安全。

posted on 2007-06-04 17:44  new2008  阅读(2133)  评论(0编辑  收藏  举报

导航