Simple instance analysis based on XMLHTTP in WEB

Source: Internet
Author: User
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.



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.