Zero-basic learning AJAX framework, learning AJAX framework

Source: Internet
Author: User

Zero-basic learning AJAX framework, learning AJAX framework

The above (Introduction and basics of AJAX without basic learning) provides a detailed introduction and basic application of AJAX asynchronous request server. It can be seen that some ajax processes remain unchanged. It is not necessary to write the sending code every time a request is sent. Some ajax developers have encapsulated their process into an ajax framework.

This section describes the ajaxLib and ajaxGold frameworks.

1. ajaxLib

AjaxLib is a very small ajax framework.
Use it to first introduce the file usage to the page. The framework is a framework for directly retrieving XML. The scheduling function is as follows:

LoadXMLDoc (url, callback, boolean)
The url is the address of the asynchronous request, and callback is the name of the function that is called after the request is successful. boolean indicates whether to remove spaces in the XML document. true indicates that spaces are removed.

For example:

<Input type = "button" value = "display" onclick = "loadXMLDoc ('1-7. aspx ', decodeXML, false);"/>
The XML document returned by the AjaxLib framework is saved in the global variable resultXML. You can write a program in decodeXML to analyze it. For example:

Copy codeThe Code is as follows:
Function decodeXML (){
Var oTemp = resultXML. getElementsByTagName ("temp ");
Document. getElementById ("targetID"). innerHTML = oTemp [0]. firstChild. nodeValue;
}

We can see that the code length is much shorter than before.

2. Use ajaxGold

Ajaxgold is another practical ajax framework.

Ajaxgold is another practical ajax framework. It has four functions for developers to use.

Copy codeThe Code is as follows:
GetDataReturnText (url, callback );
GetDataReturnXML (url, callback );
PostDataReturnText (url, data, callback );
PostDataReturnXML (url, data, callback );

The first two functions are used to return text and XML in get mode, and the last two functions are used to return text and XML in POST mode. The following uses postDataReturnText (url, data, callback) as an example to describe

Copy codeThe Code is as follows:
<Form>
<Input type = "button" value = "request data" onclick = "postDataReturnText ('1-8. aspx ', 'a = 2 & B = 3', display); ">
</Form>
<Div id = "targetID"> the extracted data will be displayed here </div>

The above code sends data to 1-8.aspx and transmits data a = 2b = 3. After the server returns a successful result, the function display () is called to process the returned value.

In ajaxGold, the returned text is used as the unique parameter of the callback function. Therefore, the display () function can be written in this way.

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function display (text ){
Document. getElementById ("targetID"). innerHTML = text;
}
</Script>

The above is all the content of this article. I hope you will like it.

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.