web.html(用户端):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>11</title>
<script type="text/javascript" src="jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="ajaxindex.js"></script>
</head>
<body>
<input type="text" id="namevalue">
<br>
<button type="" id="btn">Send</button>
结果:<span id="result"></span>
</body>
</html>
ajaxindex.js
$(document).ready(function(){ $("#btn").on("click",function(){ $("#result").text("请求数据中,请稍后...."); $.post('/test/server.php',{name:$("#namevalue").val()},function(data){
alert("hello");//sleep(5);
$("#result").text(data);
}).error(function(){ //get
$("#result").text("通讯有问题");
}
);//error .请求的服务器地址不对
});//on });//ready
服务器端(server.php):
<?php if(isset($_POST['name'])){
echo 'hello'.'!~~~~~~~~~~~~~~'.$_POST['name'];
}else{
echo 'Args Error';
} ?>