post() 方法通过 HTTP POST 请求从服务器载入数据。
语法:
$.post(url,data,success(data, textStatus, jqXHR),dataType)
get() 方法通过远程 HTTP GET 请求载入信息
语法:
$.get(url,data,success(response,status,xhr),dataType)
POST和GET方式差不多,下面只举get的例子。pos就只需要把get化成post
一、通过get方式请求加载
index.html
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
</head>
<body>
<a href="#" id="getData">获取</a>
<div id="content">这里显示ajax请求内容</div>
</body>
</html>
<script>
$("doucument").ready(function(){
$("#getData").click(function(){
$.get("ajax.php",{name:"lgx",sex:"boy"},function(result){
$("#content").html(result);
});
});
});
</script>
ajax.php
<?php
$name = $_GET['name'];
$sex = $_GET['sex'];
echo "your name is ".$name .'and sex is '.$sex;
//注意服务端要输出数据,客户端才能接收到。不能够用return哦