You did not read the wrong title, this article is indeed speaking Baidu Ajax, but a long time ago version of the.
Because the jquery Ajax module has the 800+ line, but the core function Jquery.ajax has the 380+ line, the direct analysis this piece of code is very easy to be confused by the code logic.
So we first analyze a simple Ajax code, from the early Baidu Tangram project.
This is the first time to review the knowledge of Ajax.
baidu.ajax.request Separate version
Copy Code code as follows:
/**
* Send an AJAX request
* @author: Allstar, Erik, Berg
* @name Ajax.request
* @function
* @grammar ajax.request (url[, Options])
* @param {string} URL to send the requested URL
* @param {Object} options parameter to send request
* @config the type that {String} [method] requests to send. Default to get
* @config {Boolean} [async] is asynchronously requested. The default is True (asynchronous)
* @config the data that {String} [data] needs to send. If it is a GET request, this property is not required
* @config HTTP request header to be set by {Object} [headers]
* @config {number} [Timeout] timeout time, unit ms
* @config {String} [Username] User Name
* @config {String} [password] password
* @config {function} [onsuccess] is fired when a request succeeds, function (XMLHttpRequest xhr, String responsetext).
* @config {function} [OnFailure] request fails to trigger, Function (XMLHttpRequest xhr).
* @config {function} [onbeforerequest] fires before the request is sent, Function (XMLHttpRequest xhr).
*
* @meta Standard
* @see Ajax.get,ajax.post
*
* @returns {XMLHttpRequest} send the requested XMLHttpRequest object
*/
var ajax = {};
Ajax.request = function (Url,options,type) {
Do you need asynchronous
var async = options.async| | True
User name, password
Username = options.username| | "",
Password = options.password| | "",
Data that needs to be transferred
data = options.data| | "",
Get or Post
Method = (options.method| | " Get "). toUpperCase (),
Request headers
headers = options.headers| | {},
Event handler function table
EventHandler = {},
Request data type
DataType = type| | " String ";//xml| | String
function Statechangehandler () {
See if it's ready.
if (xhr.readystate = = 4) {
Get XHR Current state
var sta = xhr.status;
To judge whether or not to succeed
if (STA = = 200| | STA = = 304) {
Success triggers success
Fire ("success");
}else{
Failure triggers failure
Fire ("failure");
}
Clear Bindings
Window.settimeout (function () {
Xhr.onreadystatechange= new Function ();
if (async) {
XHR = null;
}
},0);
}
}
function Fire (type) {
Turn the type into Ontype
Type = "On" +type;
To find the handler function for the corresponding event in the event handler table
var handler = Eventhandler[type];
If the function exists, the
if (handler) {
Not successful.
if (Type!= "onsuccess") {
Handler (XHR);
It worked
}else{
Returns different data depending on the datatype
Handler (xhr,datatype!= "XML" Xhr.responseText:xhr.responseXML);
}
}
}
Assembly EventHandler
for (var key in options) {
Eventhandler[key] = Options[key];
}
Create a new XMLHttpRequest object
var xhr = new XMLHttpRequest ();
If the method is get, assemble the data into the URL
if (method = = "Get") {
url = (Url.indexof ("?") >=0)? " & ":" "?";
URL + + data;
Clear Data
data = null;
}
If it is asynchronous
if (async) {
Processors bound to ReadyStateChange
Xhr.onreadystatechange = Statechangehandler;
}
See if you need to enter a password
if (username) {
Xhr.open (METHOD,URL,ASYNC,USERNAME,PASSOWRD);
}else{
Xhr.open (Method,url,async);
}
If it's post
if (method = = "POST") {
Set up the request header
Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
}
Set the request header information in the options to all
for (var key in headers) {
Xhr.setrequestheader (Name,headers[key])
}
Triggering event Beforerequest
Fire ("Beforerequest");
Send data
Xhr.send (data);
If it is not asynchronous
if (!async) {
Then run Statechangehandler directly to process the data
Statechangehandler ();
}
return XHR;
}
This code is relatively easy to understand.:
• Create a new XMLHttpRequest object by XMLHttpRequest ().
• See if it is get, or post, if it is get, assemble the URL, and if it is post, set the request header.
• See if it is asynchronous, if it is, register the listener function Statechangehandler.
• See if you need a username and password to execute open.
• Send request.
• Wait for the listener function to handle the event.
Baidu.ajax.get & Baidu.ajax.post
Copy Code code as follows:
/**
* Send a POST request
* @name Ajax.post
* @function
* @grammar ajax.post (URL, data[, onsuccess])
* @param {string} URL to send the URL address of the request
* @param data sent by {string}
* @param {function} [Onsuccess] callback function after successful request, function (XMLHttpRequest xhr, String responsetext)
* @meta Standard
* @see Ajax.get,ajax.request
*
* @returns {XMLHttpRequest} send the requested XMLHttpRequest object
*/
Ajax.post = function (url,data,onsuccess) {
Return Ajax.request (url,{"Data":d ata, "onsuccess": Onsuccess,method: "POST"});
}
Copy Code code as follows:
/**
* Send a GET request
* @name Ajax.get
* @function
* @grammar Ajax.get (url[, onsuccess])
* @param {string} URL to send the URL address of the request
* @param {function} [Onsuccess] callback function after successful request, function (XMLHttpRequest xhr, String responsetext)
* @meta Standard
* @see Ajax.post,ajax.request
*
* @returns {XMLHttpRequest} send the requested XMLHttpRequest object
*/
Ajax.get = function (url,data,onsuccess) {
Return Ajax.request (url,{"Data":d ata, "onsuccess": onsuccess});
}
Baidu.ajax.get and Baidu.ajax.post are all extended through baidu.ajax.request.