Ajax basics 1 and ajax 1

Source: Internet
Author: User
Tags ajax status code

Ajax basics 1 and ajax 1

For website developers, there will be no stranger to Ajax technology. This article will let us know about it, maybe we can use it in the future development process. What about Ajax in that regard? Answer: form registration, traditional form registration, sometimes requires a large amount of information. When we finish writing it easily and click submit, the background will find that some of our fields are incorrectly filled, this will prompt us that we failed to register. Please register again. In this case, I think you must hate this website like me. How can we optimize this process? At this time, our Ajax will be used. When we fill in a line of information, we can use Ajax for asynchronous background judgment. After talking about this, we can simply say that Ajax mentioned in this Article can implement asynchronous operations. Well, let's take a look at it.

1. Create Ajax:

When Ajax is required, you can create an XMLHttpRequest object in javascript to process the object. Here, we need to note that some Browsers Do not support Ajax. At this time, when we create an object, we must make certain judgments to ensure that our pages are normally displayed on different browsers.

If (window. XMLHttpRequest) {// check whether the browser version supports hppt = new XMLHttpRequest (); // IE7 +; Firefox; Chrome; Opera} else {
Hppt = new ActiveXObject ("Microsoft. XMLHTTP"); // IE6; IE5
}

2. Form Interface Design:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

3. Ajax status code:

ReadyState status description
(0) not initialized
In this phase, check whether the XMLHttpRequest object is created and make preparations for calling the open () method for initialization. If the value is 0, the object already exists. Otherwise, the browser reports an error -- the object does not exist.
(1) Load
In this phase, the XMLHttpRequest object is initialized, that is, the open () method is called, and the object state is set according to the parameter (method, url, true. And call the send () method to start sending requests to the server. If the value is 1, a request is being sent to the server.
(2) Load completed
In this phase, the server receives response data. However, the raw data returned by the server cannot be directly used on the client. If the value is 2, all response data has been received. Prepare for data parsing in the next stage.
(3) Interaction
This phase parses the received server response data. That is, the data is converted into a format that can be accessed through the responseBody, responseText, or responseXML Attribute Based on the MIME type returned by the response header of the server, so as to prepare for the client call. Status 3 indicates that the data is being parsed.
(4) Completed
At this stage, it is confirmed that all data has been resolved to the available format of the client, and the resolution has been completed. If the value is 4, the data is parsed. You can use the corresponding attributes of the XMLHttpRequest object to obtain the data.
In summary, the lifecycle of the XMLHttpRequest object should include the following stages: Create, initialize, send, receive, and parse data.

4. obtain the corresponding method:

  

 

5. select class: used to receive data transmitted by Ajax.

public class ajax extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        request.setCharacterEncoding("utf-8");        response.setCharacterEncoding("utf-8");        String name = request.getParameter("name");        name = new String(name.getBytes("iso-8859-1"), "utf-8");        String pwd = request.getParameter("pwd");        System.out.println("name="+name);        System.out.println("pwd="+pwd);        System.out.println("GET_______Ajax");    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {                request.setCharacterEncoding("utf-8");        response.setCharacterEncoding("utf-8");        String name = (String) request.getParameter("name");        System.out.println("name="+name);        String sex = (String)request.getParameter("sex");        System.out.println("sex="+sex);        System.out.println("POST__________Ajax");            }}

Now, let's get started with the basic Ajax knowledge.

 

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.