AJAX development of a learning Notes
Related Tags:
AJAX Development Framework:
A: initialization and issued XMLHttpRequest object request
To enable JavaScript can be sent Http requests to the server, you must use XMLHttpRequest object. Before, we should first XMLHttpRequest object instantiation. Because of the different browsers, different examples of the process, IE ActiveX controls to the form, such as Mozilla browser directly to the form of XMLHttpRequest. To allow the process to the preparation of cross-browser running, it is necessary to write this.
If (window.XMLHttpRequest)
(/ / Mozilla
Http_request = new XMLHttpRequest ();
)
Else if (window.ActiveXObject)
(/ / IE
Http_request = new ActiveXObject ( "Microsoft.XMLHTTP");
)
Some Mozilla browsers handle server does not include the return of XML mime-type head of the information content would be wrong. So to ensure that the return of content includes text / xml information.
Http_request = new XMLHttpRequest ();
Http_request.overrideMimeType ( "text / xml");
B: designated corresponding processing function
When the server returned to the specified client information in the way. As long as the corresponding handling assign XMLHttpRequest function of the object can be a onreadystatechange property. For example:
Http_request.onreadystatechange = processRequest;
ProcessRequest which this function without brackets were not specified parameters. Javascript can also use the function definition of immediate manner corresponding function.
For example:
Http_request.onreadystatechange = function (){};
C: Http requests issued
Designated function after treatment response can be sent to the server Http request. This step called XMLHttpRequest object of the open and send methods.
For example:
Http_request.open ( 'GET', 'http://www.example.org/some.file', true);
Http_request.send (null);
Open function of a parameter of the request is Http: Get, Post, Head
Open function of the second parameter is the target url. For safety considerations, only with the url of this domain, otherwise, the authority will be prompted not wrong. XMLHttpRequest target URL address the request and the general Http request,
Open function of the third parameter is waiting for instructions whether to return to the information server within the time following the implementation of the code. If it is true it is not implemented until the server returned to the information. The default is true.
Before calling send function, if transmission of documents, or to Post content to the server, they must first call setRequestHeader, modify MIME types.
For example:
Http_request.setRequestHeader ( "Content-Type", "application / x-www-form-urlencoded");
At this time enquiries to the information given in the form of string, as a send parameters.
Such as: name = value & anothername = = othervalue & so on
D: Back to information processing server
B has been designated to deal with the return of information designated function: processRequest
First, check the XMLHttpRequest object readyState values, the current state of judgement request. Reference to the former attribute table that the value of readyState 4, has been transferred back to the server on behalf of all the information, and deal with information and updates page content.
For example:
If (http_request.readyState == 4)
(
/ / Information has been returned, can be dealt with
)
Else
(
/ / Information not to return, waiting for
)
Return information to the server after the return of judgement Http state code to determine the return of pages do not wrong. Including 200 representatives of the normal pages.
For example:
If (http_request.status == 200)
(
/ / Normal pages, can begin to process information
)
Else
(
/ / Page problem
)
XMLHttpRequset information on the successful return of the two approaches.
1, responseText: the information will be sent back using as a string.
2, responseXML: the information will be sent back as the use of XML documents can be used to deal with DOM.
E: a preliminary framework for the development of
Summarize the above steps, we sorted out a preliminary framework for the development of the available for future calls. Here, the server will return the information to use window.alert displayed in the form of string
Code as follows:
<script Language="javascript">
<! --
Var http_request = false;
Function send_request (url)
(
/ / Initialization, the designated processing function, a function of the request sent
Http_request = false;
/ / Initialization started XMLHttpRequest object
If (window.XMLHttpRequset)
(
/ / Mozilla browser
Http_request = new XMLHttpRequest ();
If (http_request.overrideMimeType)
(
/ / Set the Mime category
Http_request.overrideMimeType ( "text / xml");
)
)
Else if (window.ActiveXObject)
(
/ / IE browser
Try
(
Http_request = new ActiveXObject ( "Msxml2.XMLHTTP");
)
Catch (e)
(
Try
(
Http_request = new ActiveXObject ( "Microsoft.XMLHTTP");
)
Catch (e) ()
)
)
If (! Http_request)
(
/ / Anomaly, the failure to create instances
Window.alert ( "can not create XMLHttpRequest instances");
Return false;
)
Http_request.onreadystatechanage = processRequest;
/ / Determine the modalities and sent the request URL, as well as whether the implementation of the code under paragraph
Http_request.open ( 'GET', url, true);
Http_send (null);
)
/ / Return information processing function
Function processRequest ()
(
If (http_request.readyState == 4)
(
/ / Object state judge
If (http_request.status == 200)
(
/ / Information has been successfully returned to start processing information
Alert (http_request.responseText);
)
Else
(
/ / Pages is not normal
Alert ( "You have an unusual request pages");
)
)
)
-->
</ Script>
Next use the above framework to be a simple application.
In the form of user registration, user name often is the only test, the traditional practices will be conveyed to the server-side data together to start verification, such transmission of data, server load, corresponding slow, if the user name already exists, the customer information while filling weight, it is laborious. AJAX use, users can be sent to a separate asynchronous server-side validation, the client need to refresh the page.
Code as follows:
<form Name="form1" action="" method="post">
Username: <input type = "text" name = "username" value =""/>< br>
<input Type="button" name="check" value="唯一性检验" onClick="userCheck()"/>
<input Type="submit" name="submit" value="Submit"/>
</ Form>
In the framework of the above add a function call:
Function userCheck ()
(
Var f = document.form1;
Var username = f.username.value;
If (username =="")
(
Alert ( "ID can not be empty");
F.username.focus ();
Return false;
)
Else
(
Send_request ( "sample1_2.jsp? Username =" + username);
)
)
Let us look at the sampel1_2.jsp done.
<% @ Page contentType = "text / html; gb2312 charset ="%>
<%
String username = request.getParameter ( "username");
If ( "hellouser." Equals (username))
Out.print ( "user name already exists, a replacement");
Else
Out.print ( "change my user id have not registered yet, you can continue to");
%>
Without operation of the refresh pages.
- Css learning Notes (1)
- Css learning pace
- Learning css 4
- Five learning css
- CSS learning from entry to the master
- Css learning my feelings!
- Css learning some experience
- Learning css 3
- Learning CSS
- CSS cascading style of learning [1]
- CSS learning
- Css learning in the Filter
- Css learning (1)
- CSS cascading style of learning [2]
- Css learning Notes
- CSS layout of the learning some ideas
- Css learning Notes - yyy431706 (of Portland)
- CSS learning should pay attention to learning method
- CSS learning "text attributes"
- CSS layout learning experience




