ajax的两种应用方式

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
//jquery方式
function qingqiu(){
 $.ajax({
    type: "POST",
    url: "test.php",
    data: "name=John&location=Boston",
    //data: {name:'John',location:'Boston'},
    success: function(msg){
    $(".show").html(msg);
    }
 });
}

//原生javascript
function qingqiu2(){
 var xmlhttp;
 if (window.XMLHttpRequest){
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
 }
 else{
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }

 xmlhttp.onreadystatechange=function(){
   if (xmlhttp.readyState==4 && xmlhttp.status==200){
  document.getElementByIdx_x("myDiv").innerHTML=xmlhttp.responseText;
   }else if(xmlhttp.status==404){
  document.getElementByIdx_x("myDiv").innerHTML='未找到页面';
   }else{
  document.getElementByIdx_x("myDiv").innerHTML='loading...'; 
   }
 }

 xmlhttp.open("POST","test.php",true);
 xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 xmlhttp.send('name=John&location=Boston');
 
}

</script>
</head>

<body>
<a href="#" class="" onclick="qingqiu2()">请求</a>

<div class="show" id="myDiv"></div>
</body>
</html>

test.php

<?php
header('Content-Type: text/html; charset=utf-8');

$name=$_POST['name'];
$location=$_POST['location'];
print($name.':'.$location."<br />\n");
?>

原文地址:https://www.cnblogs.com/sheshou/p/5201427.html