Simple example analysis based on XMLHTTP in web (figure)

Source: Internet
Author: User
Tags format array execution header string
Web|xml
   Introduction
  
We all know that you can get the data from form forms by post or gets, so how do we get the data out of the page without refreshing the submission? This is going to be through the XMLHTTP agreement. XMLHTTP is part of the XMLDOM technology.
  
The following code is a very simple example, we use XMLHTTP technology to achieve a simple user login.
  
   Start
  
1. Simple login Page
  
login.jsp
function Toserver () {
var xml = "<root>" +
"<name>" +document.all (' name '). value+ "</name>" +
"<pwd>" +document.all (' pwd '). value+ "</pwd>" +
"</root>";
  
var xmlsender = new ActiveXObject ("Microsoft.XMLHTTP");
Xmlsender.open ("POST", ' do_login.jsp ', false);
Xmlsender.send ((XML));
alert (Xmlsender.responsetext); Can handle the results returned by the background
}
  
Name: <input type= "text" id= "name"/><br>
Password: <input type= "text" id= "pwd"/><br>
<input type= "button" value= "Login" >
  
2. Background of the Login processing page
do_login.jsp
  
<%
Read XMLHTTP Stream
Java.io.BufferedReader br = Request.getreader ();
String str = "";
while (str!= null) {
str = Br.readline ();
Process (str); Parsing XML can be implemented 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
  
With the traditional "submit-postback-redraw" type of web system basic operating structure is different, we can through XMLHTTP to achieve without a refreshing client directly with the server interaction, greatly improve the user's feelings.
  
   reference materials
  
xmlHTTP Method:
  
Open Bstrmethod, bstrURL, Varasync, Bstruser, Bstrpassword
Bstrmethod: Data transfer mode, i.e. get or post.
bstrURL: URL of the Service Web page.
Varasync: Whether to execute synchronously. The default is true, which is synchronous execution, but can only be implemented synchronously in the DOM.
It is generally set to false in the application, that is, asynchronous execution.
Bstruser: User name, can be omitted.
Bstrpassword: User password, can be omitted.
  
Send Varbody
Varbody: Instruction set. can be XML format data, or it can be a string, a stream, or an unsigned integer array. You can also omit the instruction by substituting the URL parameter for the open method.
  
setRequestHeader Bstrheader, Bstrvalue
Bstrheader:http Header (header)
Value of Bstrvalue:http header (header)
If the open method is defined as post, you can define a form to upload:
Xmlhttp.setrequestheader "Content-type", "application/x-www-form-urlencoded"
  
XMLHTTP Properties:
  
onReadyStateChange: Gets the event handle that returns the result in the synchronous execution mode. Can only be invoked in the DOM.
Responsebody: The result returns an array of unsigned integers.
Responsestream: The result is returned as a IStream stream.
ResponseText: The result is returned as a string.
Responsexml: Results are returned as XML format data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.