Since JavaScript has a variety of frameworks, such as jquery, using Ajax has become quite simple. But sometimes in order to pursue simplicity, it is possible that the project does not need to load jquery such a large JS plug-in. But what about the ability to use AJAX? Here's a couple of ways to use JavaScript to implement native Ajax.
You must create a XMLHttpRequest object before you implement Ajax. If you do not support the browser that created the object, you need to create ActiveXObject, as follows:
| The code is as follows |
Copy Code |
var xmlHttp;
function Createxmlhttprequest () {
if (window. ActiveXObject) {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
else if (window. XMLHttpRequest) {
Xmlhttp=new XMLHttpRequest ();
} |
(1) Use the XMLHTTP created above to implement the simplest Ajax GET request:
| The code is as follows |
Copy Code |
|
function doget (URL) {
Note that when passing the parameter value, it is best to use encodeURI to deal with it, in case garbled
Createxmlhttprequest ();
Xmlhttp.open ("get", url);
Xmlhttp.send (NULL);
Xmlhttp.onreadystatechange = function () {
if ((xmlhttp.readystate = 4) && (Xmlhttp.status = = 200)) {
Alert (' success ');
} else {
Alert (' fail ');
}
}
} |
(2) Use the XMLHTTP created above to implement the simplest Ajax POST request:
| code is as follows |
copy code |
function DoPost (url,data) {
Note that when passing the parameter value, it is best to use encodeURI to deal with it, in case garbled
Createxmlhttprequest ();
Xmlhttp.open ("POST", url);
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xmlhttp.send (data);
Xmlhttp.onreadystatechange = function () {
if ((xmlhttp.readystate = 4) && (Xmlhttp.status = = 200)) {
Alert (' success ');
} else {
Alert (' fail ');
}
}
} |
Here's an encapsulation of the $.ajax method of simulating jquery that you'll see online:
| The code is as follows |
Copy Code |
|
var createajax = function () {
var xhr=null;
try {
Xhr=new ActiveXObject ("Microsoft.XMLHTTP"); IE Series Browser
} catch (E1) {
try{
Xhr=new XMLHttpRequest (); Non IE browser
} catch (E2) {
Window.alert ("Your browser does not support Ajax, please replace it!") ");
}
}
return XHR;
};
var ajax=function (conf) {
var Type=conf.type; type parameter, optional
var Url=conf.url; URL parameter, required
var data=conf.data; The data parameter is optional and is required only on post requests
var Datatype=conf.datatype; DataType parameter Optional
var success=conf.success; callback function optional
if (type = = null) {
Type= "Get"; The type parameter is optional and the default is get
}
if (DataType = = null) {
Datatype= "Text"; DataType parameter optional, default to Text
}
var xhr = Createajax ();
Xhr.open (type,url,true);
if (type== "Get" | | | type== "GET") {
Xhr.send (NULL);
else if (type== "POST" | | | type== "POST") {
Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xhr.send (data);
}
Xhr.onreadystatechange=function () {
if ((xhr.readystate = 4) && (Xhr.status = = 200)) {
if (datatype== "text" | | | datatype== "text") {
if (success!= null) {
Success (Xhr.responsetext); Plain text
}
else if (datatype== "xml" | | | datatype== "XML") {
if (success!= null) {
Success (Xhr.responsexml); Receive XML document
}
else if (datatype== "JSON" | | datatype== "JSON") {
if (success!= null) {
Success (eval ("+xhr.responsetext+")); Converts a JSON string to a JS object
}
}
}
};
}; |
The method is also simple to use, as is the $.ajax method of jquery, but not so many parameters. It simply implements some basic AJAX features. Use the following methods:
| code is as follows |
copy code |
|
Ajax ({
& nbsp; type: "POST",//post or get, not required
URL: "test.jsp",//Required
Data: "Name=dipoo&info=good",//Not required
dataType: "JSON",//text/xml/json, not required
Success:function (data) {//callback function, not required
alert (data.name);
}
}); |