HTML
<h2>AJAX</h2> <button type="button" onclick="loadXMLDoc()">请求数据</button> <div id="myDiv"></div>
原生ajax
function loadXMLDoc() { var xhr; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xhr = new XMLHttpRequest(); } else {// code for IE6, IE5 xhr = new ActiveXObject("Microsoft.XMLHTTP"); } xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { console.log(xhr.responseText); document.getElementById("myDiv").innerHTML = xhr.responseText; } } xhr.open("POST", "ajax1.php", true); xhr.send(); }
php部分
<?php echo "使用POST方法请求得到的<br/>"; echo date("Y-m-d h:i:s",time()); ?>
点击按钮之前
点击按钮之后