这是客户端的代码:
$(document).ready(function(){
$("button").click(function(){
$.ajax({
url: 'server.php',
type: 'POST',
timeout: 30000,
error: function(){
alert('请求出错');
},
success: function(msg){
alert(msg);
}
});
});
});
</script>
服务端代码:
<?php
$user = $_POST['username'];
$pass = $_POST['password'];
//连接数据库
mysql_connect('xxx','xxx','xxx') or die("数据库连接失败");
//设置编码
mysql_query('set names utf8');
//选择数据库
mysql_query("USE upin");
$res = mysql_query("SELECT * FROM upin_production_area");
$rows = array();
//获取结果集
while($row = mysql_fetch_array($res)){
$rows[] = $row;
}
//json编码
echo json_encode($rows);
我只知道大概是这个原理,不知道具体怎么做规范,求教!
RESTful API 设计指南[阮一峰]
http://www.ruanyifeng.com/blog/2014/05/restful_api.html