html代码:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
div{width:100px;height:100px;border:1px solid red;}
</style>
<script type="text/javascript" src="../jquery.1.9.1.min.js"></script>
</head>
<body>
<div onclick=‘test1();‘>post</div>
<div onclick=‘test2();‘>get</div>
<div onclick=‘test3();‘>ajax</div>
<script type="text/javascript">
function test1(){
var pft = 333;
var ads = ‘sss‘;
$.post(‘./post.php‘,{‘page‘:‘1‘,‘profit‘:pft,‘address‘:ads},function(data){
alert(data.page);
alert(data.profit);
alert(data.address);
alert(data.arr.id);
alert(data.arr.name);
}, "json");//设置了获取数据的类型,所以得到的数据格式为json类型的
}
function test2(){
var pft = 444;
var ads = ‘sss‘;
$.get(‘./get.php‘,{‘page‘:‘2‘,‘profit‘:pft,‘address‘:ads},function(data){
alert(data.page);
alert(data.profit);
alert(data.address);
alert(data.arr.id);
alert(data.arr.name);
}, "json");//设置了获取数据的类型,所以得到的数据格式为json类型的
}
function test3(){
var pft = 555;
var ads = ‘sss‘;
$.ajax({
url:‘./ajax.php‘,
type:"post", //请求的格式
data:{‘page‘:‘3‘,‘profit‘:pft,‘address‘:ads},
async:false, //异步
dataType:‘json‘,
success:function(data){
alert(data.page);
alert(data.profit);
alert(data.address);
alert(data.arr.id);
alert(data.arr.name);
},
error:function(er){
BackErr(er);
}
});
}
</script>
</body>
</html>
post.php 代码:
<?php
$page = $_POST[‘page‘];
$profit = $_POST[‘profit‘];
$address = $_POST[‘address‘];
$arr = array(
‘page‘=>$page,
‘profit‘=>$profit,
‘address‘=>$address,
‘arr‘=>array(
‘id‘=>22,
‘name‘=>‘china‘
)
);
exit(json_encode($arr));
get.php代码:
<?php
$page = $_GET[‘page‘];
$profit = $_GET[‘profit‘];
$address = $_GET[‘address‘];
$arr = array(
‘page‘=>$page,
‘profit‘=>$profit,
‘address‘=>$address,
‘arr‘=>array(
‘id‘=>22,
‘name‘=>‘china‘
)
);
exit(json_encode($arr));
ajax.php 代码:
<?php
$page = $_POST[‘page‘];
$profit = $_POST[‘profit‘];
$address = $_POST[‘address‘];
$arr = array(
‘page‘=>$page,
‘profit‘=>$profit,
‘address‘=>$address,
‘arr‘=>array(
‘id‘=>22,
‘name‘=>‘china‘
)
);
exit(json_encode($arr));