As we all know, we can use post or get to get the data of form forms. how can we directly obtain the data on the page without refreshing the submission? This requires the help of the xmlhttp protocol. Xmlhttp is part of the xmldom technique. The following code is
As we all know, we can use post or get to get the data of form forms. how can we directly obtain the data on the page without refreshing the submission? This requires the help of the xmlhttp protocol. Xmlhttp is part of the xmldom technique.
The following code is a simple example. We use xmlhttp to implement simple user login.
Start
1. simple logon page
Reference content is as follows:
Login. jsp
Function toServer (){
Var xml =" "
" "Document. all ('name'). value" "
" "Document. all ('pwd'). value" "
" ";
Var XMLSender = new ActiveXObject ("Microsoft. XMLHTTP ");
XMLSender. Open ("POST", 'Do _ login. jsp ', false );
XMLSender. send (xml ));
Alert (XMLSender. responseText); // processing results returned by the background
}
Name:
Password:
2. logon processing page in the background
Reference content is as follows:
Do_login.jsp
<%
// Read the XMLHTTP stream
Java. io. BufferedReader br = request. getReader ();
String str = "";
While (str! = Null ){
Str = br. readLine ();
Process (str); // parse XML in any language for business processing
}
// Return information
Javax. servlet. ServletOutputStream sos = response. getOutputStream ();
Sos. print ("login success ");
Sos. close ();
%>
3. the results are as follows:
Summary
Unlike the basic running structure of the traditional "submit-send-re-draw" web system, we can use XMLHTTP to achieve direct interaction between the client and the server without refreshing the new one, greatly improve user experience.
Exam materials
XMLHTTP method:
Open bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword
BstrMethod: data transmission method, that is, GET or POST.
BstrUrl: The URL of the service webpage.
VarAsync: whether to perform synchronization. The default value is True, that is, synchronous performance, but can only be performed in the DOM.
In use, it is usually set to False, that is, asynchronous performance.
BstrUser: user name, which can be omitted.
BstrPassword: user password, which can be omitted.
Send varBody
VarBody: instruction set. It can be XML pattern data, a string, a stream, or an unsigned integer array. It can also be omitted, so that the command is replaced by the URL parameter of the Open method.
SetRequestHeader bstrHeader, bstrValue
BstrHeader: HTTP header)
BstrValue: HTTP header value
If the Open method is defined as POST, you can define the form method upload:
Xmlhttp. setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
XMLHTTP attributes:
Onreadystatechange: event handle for obtaining returned results under the synchronous performance method. It can only be called in the DOM.
ResponseBody: returns an unsigned integer array.
ResponseStream: The result is returned as an IStream stream.
ResponseText: returns the result as a string.
ResponseXML: The result is returned as XML format data.