Ajax call simple instance

Time:2020-11-2

This paper describes the method of Ajax call. For your reference, the details are as follows:

Fill the data returned from the URL into the div with the specified ID

client: test.html

<script>
function createXMLHttp()
{
  var ret = null;
  try {
    ret = new ActiveXObject('Msxml2.XMLHTTP');
  }
  catch (e) {
    try {
      ret = new ActiveXObject('Microsoft.XMLHTTP');
    }
    catch (ee) {
      ret = null;
    }
  }
  if (!ret && typeof XMLHttpRequest != 'undefined')
    ret = new XMLHttpRequest();
  return ret;
}
function loadXmlToElement(fragment_url, element_id)
{
 var element = document.getElementById(element_id);
 element.innerHTML  ='<img src=" loading.gif "Border = 0" / > data loading... < br / > ';
 var xmlhttp = createXMLHttp();
 xmlhttp.open("GET", fragment_url);
 xmlhttp.onreadystatechange = function() 
 {
 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
 {
  //alert(xmlhttp.responseXML.getElementsByTagName("data")[0].childNodes[0].nodeValue);
  element.innerHTML =
unescape(xmlhttp.responseXML.getElementsByTagName("data")[0].childNodes[0].nodeValue);
  if(!element.innerHTML || element.innerHTML == "/n")
  {
  try{
   element.innerHTML = unescape(xmlhttp.responseXML.firstChild.childNodes[1].nodeValue);
  }catch(e){}
  }
 }
 }
 xmlhttp.send(null); 
}
</script>
<div>
</div>
<script>
 var url="t.xml";
 //alert(url);
 loadXmlToElement(url,"data");
</script>

Fill the data returned from the URL into the div with the specified ID

Server:

test.xml


<?xml version='1.0' encoding='utf-8' ?>
<data>
<![CDATA[ 
this is a test
]]>
</data>

Note that the server returns data in CDATA format, which can be any HTML code inside

I hope this article will be helpful to everyone’s Ajax programming.

Recommended Today

SDS of redis data structure

SDS(simple dynamic string), simple dynamic string. S and it’s called hacking string. Where hack is stored is the length of the string and the remaining space in SDS. The implementation of SDS insds.cIn the middle. C language string uses a character array of length N + 1 to represent the string of length N, and […]