function get (URL, options, callback) { //define Get function
if (XMLHttpRequest) {
var xhr=new xmlhttprequest ();
}else{
var xhr=new activexobject ("Microsoft.XMLHTTP"); //compatible with IE
}
xhr. onreadystatechange = function (callback) {
if (xhr. readyState = = = 4) {
if ((Xhr.status >= && xhr.status <) | | xhr.status = = 304) {
Callback (Xhr.responsetext);
}else{
Alert ("The request was unsuccessful:" + XHR. Status)
}
}
}
var seriurl = url + '? ' + serialize (options);
XHR. Open (' Get ', Seriurl, true);
XHR. Send (NULL);
}
function serialize (data) {
if (!data) return ';
var pairs = [], value;
for (name in data) { //Traversal Object Properties
if (!data.hasownproperty (name)) continue; //Filter out properties and methods of inherited prototypes
if (typeof data[name] = = = ' function ') continue; //Filter out function method
Value = Data[name].tostring (); //attribute value to string
Name = encodeURIComponent (name); //The property name string can be encoded as a URI component. Returns a copy of the value uristring, where some of the characters are replaced by a hexadecimal escape sequence.
Value = encodeURIComponent (value); //property value for URI encoding.
Pairs.push (name + ' = ' + value); //property names and values into an array
}
Return Pairs.join (' & '); //The elements in the array are returned in string form with & separate
}
Get ('/information ', {name: ' NetEase ', age:18}, function (data) {
Console.log (data);
Processing return Data
});
The encapsulation of the AJAX request get method