Java from getting started to giving up: basic usage of XMLHttpRequest

Source: Internet
Author: User

No idle chatter, direct lecture.

Using the XMLHttpRequest object is divided into the following seven steps:

    1. Creating objects

    2. Set Expiration Time

    3. Set data format

    4. Initializing an HTTP request

    5. Set HTTP Header Requests

    6. Processing of postback data

    7. Send HTTP Request

The corresponding code is shown below

<script type= "Text/javascript" >    var xhr;     Function goajax ()  {//1. Create Object Xhr = new xmlhttprequest ();if  (window. XMLHttpRequest)  {    xhr = new xmlhttprequest ();}  else if  (window. ActiveXObject)  {    xhr = new activexobject ("Microsoft.XMLHTTP");} 2. Set expiration time XHR.SETTIMEOUT&NBSP;=&NBSP;3000;//3. Format Data xhr.responsetype =  "text";//4. Initialize  http   Request parameters (not sent) Xhr.open ("POST",  "Servlet/ajaxloginservlet",  true)//5. Set HTTP request Xhr.setrequestheader (" Content-type ", " application/x-www-form-urlencoded ");//6. Processing of postback data//registration related event callback handler function//6.1  After the postback data has been loaded, execute Xhr.onload = function (e)  {    //alert (this.readystate +   "| |"  + this.status);     if (this.readystate == 4 | | &NBSP;THIS.STATUS&NBSP;==&NBSP;200)  {var div  = document.getelementbyid ("Divcontent");d iv.innerhtml = this.responsetext;     }};//6.2 Access Error xhr.onerror = function (e)  {    alert ("Login failed! ");};/ /6.3 Timeout//xhr.ontimeout = function (e)  {//};//6.4 status change/* xhr.onreadystatechange =  Function (e) {        if (this.readystate == 4 | | &NBSP;THIS.STATUS&NBSP;==&NBSP;200)  {             alert (This.responsetext);         }} *///7. Sending Data var  Username = document.getelementbyid ("username") .value;var pwd =  document.getElementById ("password"). Value;xhr.send ("username="  + username +  "&password = " + pwd);    }</script>

The HTML page code looks like this:

<body> account: <input type= "text" name= "username" id= "username"/><br/> Password: <input type= "Password" n Ame= "password" id= "password"/><br/> <input type= "button" value= "Login" onclick= "Goajax ();"/> <div Id= "Divcontent" style= "width:200px; height:100px; " ></div></body>

The servlet file code is as follows:

public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {// Set content format Response.setcontenttype ("text/html"); PrintWriter out = Response.getwriter ();//Gets the user name and password in the URL string username = Request.getparameter ("username"); String Password = request.getparameter ("password");//Successful output success failed output FailIf ("haha". Equals (username) && "123" . Equals (password)) {out.println ("Success");} else {out.println ("fail");} Out.flush (); Out.close ();}


This article is from the "Software Thinking" blog, please be sure to keep this source http://softi.blog.51cto.com/13093971/1950888

Java from getting started to giving up: basic usage of XMLHttpRequest

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.