Basic Ajax knowledge

Source: Internet
Author: User
Tags html header
What Is Ajax? It is difficult for everyone to know about it, so we will not talk nonsense here.
But I want to repeat the history of XMLHTTP. Although I do not like Microsoft very much, it seems that Microsoft has made many interesting things, including XMLHTTP, the main character here.
In my understanding, XMLHTTP is a class that supports JavaScript to send messages to the server. First, Microsoft used ActiveX to reference this class in Internet Explorer, which is called XMLHTTP. Such a cool gameplay is born. Next, we all think this stuff is good, so mozzila and Safari provide such a class, almost all of which are called XMLHttpRequest, right ??? They all provide a method and a property, so Ajax is about to come out!
Because different browsers and Microsoft provide different methods, it is necessary to make a little effort to use XMLHTTP: creatxmlhttp
1 var createxmlhttp = function ()
2 {
3 var XMLHTTP;
4 try
5 {
6 XMLHTTP = new activexobject ("msxml2.xmlhttp ");
7}
8 catch (E)
9 {
10 try
11 {
12 XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
13}
14 Catch (Ex ){}
15}
16 if (! XMLHTTP)
17 {
18 try
19 {
20 XMLHTTP = new XMLHttpRequest ();
21}
22 catch (EE)
23 {
24 alert ("nnd, your browse doesn't support XMLHTTP, change it! ");
25}
26}
27 return XMLHTTP;
28}

This is the most comprehensive method I have seen and improved, as for "msxml2.xmlhttp" and "Microsoft. I have not checked the differences between XMLHTTP and the two items. You can use Google or Baidu to check them out. I am too lazy to check the differences over the past few days. net packaging of word controls will make me crazy, nnd, Microsoft out of which many versions AH ).
OK. Now that XMLHTTP is created, you can call the Open Method to initialize it: 1 xmldom = createxmlhttp ();
2/* the following is an open prototype:
3/xmldom. Open (http-method, URL, async, userid, password ));*/

Obviously, the open method contains five parameters. The first three parameters are required, and the last two parameters are optional (provided when the server requires authentication ). The parameter meanings are as follows:
HTTP-method:HTTP communication methods, such as get or post. Here are the differences between the two:
1. Get adds the parameter data queue to the URL referred to by the Action attribute of the submission form. The value corresponds to each field in the form and can be seen in the URL. Post is implemented through HTTP
Post mechanism: place the fields and content in the form in the HTML header and send them to the URL address referred to by the Action attribute. You cannot see this process.
2. For the get method, the server uses request. querystring to obtain the value of the variable. For the POST method, the server uses request. Form to obtain the submitted data. You can use request to obtain parameters in either of the two methods.
3. The data volume transmitted by get is small and cannot exceed 2 kb. The amount of data transmitted by post is large, which is generally not restricted by default. Theoretically, the maximum size of IIS4 is 80 KB, and that of iis5 is 100kb.
4. Low get security and high post security.
If XMLHTTP is used for submission, it seems that there is no difference between get and post 1st and 2. haha :)
 
URL:The URL of the server that receives XML data. The ASP or CGI program (or aspx) is usually specified in the URL)
Async:A boolean identifier indicating whether the request is asynchronous. If it is an asynchronous communication method (true), the client will not wait for the server's response; if it is a synchronous mode (false), the client will wait until the server returns a message to execute other operations
Userid:User ID for Server Authentication
Password:User Password for Server Authentication

   
Well, I have already submitted it, and then I should call the send method to send XML data: 1 xmldom. Send (var obj); // OBJ can be a string, DOM tree, or any data stream.

Data transmission methods can be synchronous or asynchronous. In asynchronous mode, once a packet is sent, the send process is terminated and the client performs other operations. In synchronous mode, the client waits until the server returns a confirmation message to terminate the send process.

Here we will focus on the readystate attribute in the XMLHTTP object,It can reflect the progress of the server when processing requests. The client program can set corresponding event handling methods based on the status information. The attribute values and their meanings are shown in the following table: Value description
0 response object has been created, but the XML Document Upload process has not been completed (the request was not sent (before open () was called ))
1. the XML file has been loaded (the request has been created but has not been sent (before sending () is called ))
2. the XML file has been loaded and is being processed. (The request has been sent and is being processed.
)
3. Some XML documents have been parsed (the request has been processed, and some data is usually available, but the server has not completed the response)
4. After the documents have been parsed, the client can accept the returned message (the response is complete and the server can be accessed to respond and use the data)

The client processes the response information. After receiving the returned message, the client simply processes it and basically completes an interaction cycle between C/S. The client receives the response through the attributes of the XMLHTTP object:
Responsetext: uses the returned message as a text string;
Responsebody: uses the returned message as the content of the HTML document;
Responsexml: regards the returned message as an XML document, which is used when the server response message contains XML data;
Responsestream: treats the returned message as a stream object.

Do not attach:

XMLHTTP status attribute: 200: indicates that everything goes smoothly. 404: The page cannot be found.
A buddy on the Internet said that if the local test is normal or the page is not found, the value 0 will be returned, but I have never met it. Writing it here may help you.

Related Article

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.