SoFunction
Updated on 2025-04-05

jQuery ajax (review)—Baidu ajax request separate version


/**
* Send an ajax request
* @author: allstar, erik, berg
* @name
* @function
* @grammar (url[, options])
* @param {string} url The requested url
* @param {Object} options option parameters for sending request
* @config {String} [method] The type of request to send. Default is GET
* @config {Boolean} [async] Whether to request asynchronously. Default is true (asynchronous)
* @config {String} [data] The data to be sent. If it is a GET request, this property is not required
* @config {Object} [headers] http request header to set
* @config {number} [timeout] Timeout, unit ms
* @config {String} [username] Username
* @config {String} [password] Password
* @config {Function} [onsuccess] Triggered when the request is successful, function(XMLHttpRequest xhr, string responseText).
* @config {Function} [onfailure] Triggered when the request fails, function(XMLHttpRequest xhr).
* @config {Function} [onbeforerequest] Triggered before sending the request, function(XMLHttpRequest xhr).
*
* @meta standard
* @see ,
*
* @returns {XMLHttpRequest} XMLHttpRequest object that sends the request
*/
var ajax = {};
= function(url,options,type){
// Is it necessary to be asynchronous
var async = ||true,
// Username and password
username = ||"",
password = ||"",
// Data to be transmitted
data = ||"",
// GET or POST
method = (||"GET").toUpperCase(),
// Request header
headers = ||{},
// Event handler function table
eventHandler = {},
// Request data type
dataType = type||"string";//xml||string
function stateChangeHandler(){
// See if it's ready
if( == 4){
// Get xhr's current status
var sta = ;
// Determine whether it is successful
if(sta == 200||sta == 304){
// Success will trigger success
fire("success");
}else{
// Failure triggers failure
fire("failure");
}
// Clear binding
(function(){
= new Function();
if (async){
xhr = null;
}
},0);
}
}
function fire(type){
// Turn type into ontype
type = "on"+type;
// Find the corresponding event handler function in the event handler table
var handler = eventHandler[type];
// If the function exists
if(handler){
// If it fails
if(type != "onsuccess"){
handler(xhr);
// Success
}else{
// Return different data according to dataType
handler(xhr,dataType!="xml"?:);
}
}
}
// Assemble 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 += (("?")>=0)?"&":"?";
url += data;
// Clear data
data = null;
}
// If it is asynchronous
if (async){
// The processor that binds to readystatechange
= stateChangeHandler;
}
// Check if you need to enter a password
if(username){
(method,url,async,username,passowrd);
}else{
(method,url,async);
}
// If it is POST
if(method == "POST"){
// Set the request header
("Content-Type", "application/x-www-form-urlencoded");
}
// Set all the request header information in options
for(var key in headers){
(name,headers[key])
}
// Trigger event beforerequest
fire("beforerequest");
// Send data
(data);
// If not asynchronous
if (!async){
// Run stateChangeHandler directly to process data
stateChangeHandler();
}
return xhr;
}