一、下载phprpc文件并安装:
下载地址:https://github.com/old-blueday/phprpc
二、服务端(后台)代码
test.php文件代码
require_once("phprpc_server.php");
class test{
public function test($data){
return "hello word + "$data;
}
public function test1($id,$title){
return $id."+".$title;
}
}
$server = new PHPRPC_Server();
$server->add('test','test1');
$server->start();
三、客户端(前端)代码
//必须加载phprpc的/compressed/phprpc_client.js文件
<script type="text/javascript" src="../phprpc_3.0.1_php/js/compressed/phprpc_client.js"></script>
<div>
<button class="SeniorCollect" onclick="phprpc();">test</button>
<button class="SeniorCollect" onclick="phprpc2();">test1</button>
</div>
<script type="text/javascript">
var rpc = new PHPRPC_Client('../api/hxz/test.php', ['test','test1']);
function showResult(result) {
console.log(result);
}
var data1 = 299;
var data2 = new Array();
data2['id']=1;
data2['title']="test";
function phprpc() {
rpc.test(data1, showResult);
}
function phprpc2() {
rpc.test1(data2['id'],data2['title'],showResult);
}
</script>
|