The implementation of ajax encoding is a milestone in the construction of the Team framework, so that the framework has a version that can be used for application debugging. In the ajax framework encoding, we mainly think of the following points:
- Framework compatibility
- Comprehensive and easy to use
- Good scalability
Framework compatibility: the compatibility of the ajax framework is mainly manifested in XMLHttpRquest object generation. There are generally three methods based on different types of browsers or libraries
- New XMLHttpRequest ();
- New ActiveXObject ("Microsoft. XMLHTTP ");
- New ActiveXObject ("Msxml2.XMLHTTP ");
The method is simple, fast, and scalable.
- This is a comprehensive method similar to jquery, such as ajax. get (), ajax. json (), and ajax. post.
- The framework uses a core method as an interface, and various methods use a unified interface for asynchronous requests. To expand the method, you only need to organize your own data format and then call the core method.
Method
- Ajax. get (url, success, error)
Parameters: |
Url |
Requested url |
Required |
Parameters: |
Success |
Callback Function for successful request |
Optional |
Parameters: |
Error |
Callback Function for request failure |
Optional |
- Ajax. json (url, success, error) // use the official json Parsing Method for better security
Parameters: |
Url |
Requested url |
Required |
Parameters: |
Success |
Callback Function for successful request |
Optional |
Parameters: |
Error |
Callback Function for request failure |
Optional |
- Ajax. xml (url, success, error) // currently not supported
Parameters: |
Url |
Requested url |
Required |
Parameters: |
Success |
Callback Function for successful request |
Optional |
Parameters: |
Error |
Callback Function for request failure |
Optional |
- Ajax. post (url, params, success, error)
Parameters: |
Url |
Requested url |
Required |
Parameters: |
Params |
Post Parameters |
Optional (null by default) |
Parameters: |
Success |
Callback Function for successful request |
Optional |
Parameters: |
Error |
Callback Function for request failure |
Optional |
- Ajax. request ({url: url, params: params, type: type, format: format, success: success, error: error })
Parameters: |
Url |
Requested url |
Required |
Parameters: |
Params |
Post Parameters |
Optional |
Parameters: |
Success |
Callback Function for successful request |
Optional |
Parameters: |
Error |
Callback Function for request failure |
Optional |
Parameters: |
Type |
Request type (POST/GET) |
Optional (GET by default) |
Parameters: |
Format |
Returned data type (json/xml/text) |
Optional (default text) |
Example
Introduction
Copy codeThe Code is as follows: <script type = "text/javascript" src = "ajax_min.js"> </script>
Request
Copy codeThe Code is as follows:
<Script type = "text/javascript">
// Define the callback function
Var callBack = function (text)
{
Alert (text );
}
// Send the request
Ajax. get ("test. php", callBack );
</Script>
Download ajax_min.js
From: cnblogs only C-Sky