<script>
var xmlHttp;
function Createxmlhttprequest () {
if (window. ActiveXObject) {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
} else if (window. XMLHttpRequest) {
XmlHttp = new XMLHttpRequest ();
}
}
function doget (URL) {
Note that when you pass the parameter value, it is best to use encodeURI to avoid garbled
Createxmlhttprequest ();
Xmlhttp.open (' GET ', url);
Xmlhttp.send (NULL);
Xmlhttp.onreadystatechange = function () {
Console.log (xmlhttp.readystate + ', ' + xmlhttp.status);
if ((xmlhttp.readystate = = 4) && (Xmlhttp.status = = 200)) {
Alert (' success ');
var data = Json.parse (Xmlhttp.responsetext);
Console.log (data);
Console.log (Data.errorcode);
} else {
Console.log (Xmlhttp.responsetext);
Alert (' fail ');
}
}
}
function doPost (URL, data) {
Note that when you pass the parameter value, it is best to use encodeURI to avoid garbled
Createxmlhttprequest ();
Xmlhttp.open (' POST ', url);
Xmlhttp.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded ');
Xmlhttp.send (data);
Xmlhttp.onreadystatechange = function () {
Console.log (xmlhttp.readystate + ', ' + xmlhttp.status);
if ((xmlhttp.readystate = = 4) && (Xmlhttp.status = = 200)) {
Alert (' success ');
Console.log (Xmlhttp.responsetext);
} else {
Console.log (Xmlhttp.responsetext);
Alert (' fail ');
}
}
}
</script>
Usage:
Doget (' Http://front/test/ajax ');
DoPost (' Http://front/test/ajax ', ' fname=bill&lname=gates ');
JavaScript implementation of native Ajax methods