First AJAX experience

Source: Internet
Author: User
HotHeart BLog: Workshop

HotHeart BLog:Www.xujiwei.cn/blog

First AJAX experience

AJAX has been a hot thing in the past two years. I have also made a great deal of effort. I went to some tutorials the other day. next I will write my own things in the whole process. However, because it is a beginner, please forgive me for any errors. thank you for your correction.

1. create an XMLHttpRequest object

There are many types of browsers, and the methods for creating XMLHttpRequest are not the same. it is considered that XMLHttpRequest is compatible with various browsers and should be considered when creating XMLHttpRequest. Currently, mainstream browsers include IE, Firefox, and Opera in Windows, so the code we write should be compatible with these browsers as much as possible. After referring to some materials, I used the following method to create the XMLHttpRequest object:

CODE:

// Define a variable and assign the initial value to false to determine whether the object is created successfully.
Var xmlObj = false;
// Apply try to capture creation failures and create them in another way
Try {
// Apply this method in Mozilla to create the XMLHttpRequest object
XmlObj = new XMLHttpRequest;
}
Catch (e ){
Try {
// If it fails, try the method in the newer IE
XmlObj = new ActiveXObject ('msxml2. xmlhttp ');
}
Catch (e2 ){
Try {
// If the request fails, try to apply the method in IE of earlier versions.
XmlObj = new ActiveXObject ('Microsoft. xmlhttp ');
}
Catch (e3 ){
// If the creation fails ......
XmlObj = false;
}
}
}
// If the XMLHttpRequest object fails to be created, the system prompts the visitor that the page may not be able to visit accurately.
If (! XmlObj ){
Alert ('xmlhttprequest init Failed! ');
}

2. apply XMLHttpRequest to obtain XML documents

When using XMLHttpRequest to obtain XML, you must note that this document must be in the same domain as yourself. I understand that it is under the same domain name or in the same directory, if this is not the case, the error "do not stop visiting" will be displayed. A Web server must be run at the local height, instead of opening the webpage directly in the browser.

CODE:

// Use the open method to open a request. this method has three parameters, namely, the request method, the request file URL, and the synchronization method (? I don't quite understand what it is :)
// The request method can be GET, POST, or HEAD. because I want to obtain the file, use GET
// Request the URL of the file and directly use the relative path.
// Synchronous method, indicating whether to wait for a response (false) after the request is sent or continue to perform the following code (true), that is, asynchronous. The first ajax a is asynchronous, so true is used here.
XmlObj. open ('GET', 'sample. XML', true );
// The asynchronous method of the application must be processed when the state of the XMLHttpRequest object changes.
XmlObj. onreadystatechange = function (){
// If The XMLHttpRequest status is 4, it should be ready, then it will be processed continuously.
If (xmlObj. readyState = 4 ){
// Determine whether the returned status is 200 OK. in some cases, if the file does not exist, the system returns 404
If (xmlObj. status = 200 ){
// Call the handling process
DoMyXML ();
}
}
}
// Send the request. because it is GET, the send content is null.
XmlObj. send (null );

3. use ASP to create XML documents

Dynamic web pages are required for dynamic display. I use ASP.

CODE:

<%
'Fixed the header identifier to indicate that this is an XML document.
Response. ContentType = 'text/XML'
'......
StrXML =' '
'Here, the content in the database is output according to the XML request.
StrXML = strXML &'.....'
'......
Response. Write (strXML)
%>

4. process XML documents

After obtaining the XML document, you need to obtain the required information from it. for example, if I have obtained the following XML document from the service:

CODE:




AJAX Study
Study AJAX

What I want is the title and content, so you can do as follows:

CODE:

Function DoMyXML (){
Var xmlDoc, items, title, content;
// Obtain the XML document from the XMLHttpRequest object first
XmlDoc = xmlObj. responseXML;
// Obtain the items
Items = xmlDoc. getElementsByTagName ('ITEM ');
// Obtain the desired content based on the TagName.
// If the XML document contains multiple items, you can use the subscript of the array to represent the first few items.
Title = items [0]. getElementsByTagName ('title') [0]. firstChild. data;
Content = items [0]. getElementsByTagName ('content') [0]. firstChild. data;
}

Now that you have obtained what I want, you can display them.

5. output processing results

Assume that the following HTML document is used to display the content to be output:

CODE:



AJAX Study




A div container with the ID of mydisplay is defined here to display the output content. after that, go to JS:

CODE:

//... Connect to DoMyXML;
// Content = items [0] ......;
Var strHTML;
// Organize the content to be displayed first
StrHTML = 'item title: 'title'
Item content: 'Content;
// Obtain the target container and set its innerHTML to the content to be displayed.
Document. getElementById ('mydisplay'). innerHTML = strHTML;

Well, none of these differences are the basis for compiling AJAX programs. the specific use depends on the individual. of course, AJAX is not the only one. For more information, see relevant materials.

Author: Origin: www.xujiwei.cn/blog First AJAX experience

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.