Java算法斐波那契数列优化(fibonacci)
2015-12-15 10:15
489 查看
<html> <head> <script type="text/javascript"> function loadXMLDoc(){ if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); //The keystone of AJAX is the XMLHttpRequest object. //All modern browsers support the XMLHttpRequest object (IE5 and IE6 uses an ActiveXObject). } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } //The onreadystatechange event is triggered four times, one time for each change in readyState. xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200)//When using async=true, //specify a function to execute when the response is ready in the onreadystatechange event: //Using async=false is not recommended, but for a few small requests this can be ok. //Remember that the JavaScript will NOT continue to execute, //until the server response is ready. If the server is busy or slow, the //application will hang or stop. { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; //If the response from the server is not XML, use the responseText property. } } xmlhttp.open("GET","ajax_info.txt",true); //to behave as AJAX, the async parameter of the //open() method has to be set to true: xmlhttp.send(); } </script> </head> <body> <div id="myDiv"><h2>Let AJAX change this text</h2></div> <button type="button" onclick="loadXMLDoc()">Change Content</button> </body> </html>
Send a Request To a Server
open(method,url,async ) | Specifies the type of request, the URL, and if the request should be handled asynchronously or not. method : the type of request: GET or POST url : the location of the file on the server async : true (asynchronous) or false (synchronous) |
send(string ) | Sends the request off to the server. string : Only used for POST requests |
A cached file is not an option (update a file or database on the server)
Sending a large amount of data to the server (POST has no size
limitations)
Sending user input (which can contain unknown characters), POST is more
robust and secure than GET
To POST data like an HTML form, add an HTTP header with setRequestHeader().
Specify the data you want to send in the send() method:
xmlhttp.open("POST","ajax_test.asp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");
setRequestHeader(header,value ) | Adds HTTP headers to the request. header : specifies the header name value : specifies the header value |
Server Response
To get the response from a server, use the responseText or responseXML propertyof the XMLHttpRequest object.
responseText | get the response data as a string |
responseXML | get the response data as XML data |
The responseText Property
If the response from the server is not XML, use the responseText property.The responseText property returns the response as a string, and you can use it accordingly:
The responseXML Property
If the response from the server is XML, and you want to parse it asan XML object, use the responseXML property:
Example
cd_catalog.xml:<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG>
Request the file cd_catalog.xml
and parse the response:
xmlDoc=xmlhttp.responseXML;
var txt="";
x=xmlDoc.getElementsByTagName("ARTIST");
for (i=0;i<x.length;i++)
{
txt=txt + x[i].childNodes[0].nodeValue + "<br />";
}
document.getElementById("myDiv").innerHTML=txt;
The onreadystatechange event
When a request to a server is sent, we want to perform some actions basedon the response.
The onreadystatechange event is triggered every time the readyState changes.
The readyState property holds the status of the XMLHttpRequest.
Three important properties of the XMLHttpRequest object:
onreadystatechange | Stores a function (or the name of a function) to be called automatically each time the readyState property changes |
readyState | Holds the status of the XMLHttpRequest. Changes from 0 to 4: 0: request not initialized 1: server connection established 2: request received 3: processing request 4: request finished and response is ready |
status | 200: "OK" 404: Page not found |
response is ready to be processed.
When readyState is 4 and status is 200, the response is ready:
相关文章推荐
- 详解ubuntu搭建Java开发环境
- [Java] 集合类克隆方法与内部排序
- Restrictions的部分用法
- 警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:esignmanage' did not find a matching property.解决
- Spring官网下载dist.zip的几种方法
- 并发引起的java.lang.NullPointerException
- Java常见异常(Runtime Exception )小结
- 生成日志文件,并在0点进行备份(java 实现)
- 关于java.lang.ThreadLocal
- Visual Assist X(Visual Studio编程的绝好助手及Eclipse的CDT
- 轻量级JavaEE企业应用实战(一)
- eclipse去除所有断点
- eclipse去除所有断点
- eclipse漂亮主题MoonRise UI,比eclipse自带的漂亮很多
- 在Eclipse上手动设置jvm参数
- ImageLoader- java.lang.OutOfMemoryError
- Java 应用程序打包成.exe
- Java线程堆栈信息查看
- java配置
- java配置