Home > Web Front-end > JS Tutorial > What are the basic common codes for ajax?

What are the basic common codes for ajax?

php中世界最好的语言
Release: 2018-04-04 14:31:20
Original
1413 people have browsed it

This time I will bring you what are the basic common codes of ajax, and what are the precautions when using the basic common codes of ajax. The following is a practical case, let's take a look.

The examples in this article describe the basic general code of ajax. Share it with everyone for your reference, the details are as follows:

<html>
<head>
<script type="text/JavaScript">
var xmlhttp
function loadXMLDoc(url)
{
 // code for Mozilla, etc.
 if (window.XMLHttpRequest)
 {
 xmlhttp=new XMLHttpRequest()
 xmlhttp.onreadystatechange=state_Change
 xmlhttp.open("GET",url,true)
 xmlhttp.send(null)
 }
 // code for IE
 else if (window.ActiveXObject)
 {
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  if (xmlhttp)
  {
  xmlhttp.onreadystatechange=state_Change
  xmlhttp.open("GET",url,true)
  xmlhttp.send()
  }
 }
}
function state_Change()
{
 // if xmlhttp shows "loaded"
 if (xmlhttp.readyState==4)
 {
 // if "OK"
 if (xmlhttp.status==200)
 {
 document.getElementById('T1').innerHTML=xmlhttp.responseText
 }
 else
 {
 alert("Problem retrieving data:" + xmlhttp.statusText)
 }
 }
}
</script>
</head>
<body onload="loadXMLDoc(&#39;test_xmlhttp.txt&#39;)">
<p id="T1" style="border:1px solid black;height:40;width:300"></p><br />
<button onclick="loadXMLDoc(&#39;test_xmlhttp2.txt&#39;)">Click</button>
</body>
</html>
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How does JSONP handle Ajax cross-domain access

Global monitoring of ajax operation, how to handle user session failure deal with

The above is the detailed content of What are the basic common codes for ajax?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template