AMF与HTTP不同,它们是不同的协议。
当使用AJAX(jQuery或不)时,您在URI上调用HTTP方法,而不是OOP方法。因此,所有内容至少在两个映射中结束:
- 将您的应用程序逻辑映射到方法和URI。
- 您的Javascript代码映射到方法和URI。
下面是使用Respect\Rest一个样本:
$router->get('/users/*', function($userName) {
return MyDatabaseLayer::fetchUser($userName); //Illustrative
})->accept(
'application/json' => function($data) {
header('Content-type: application/json');
return json_encode($data);
}
);
现在jQuery的部分:
$.getJSON('/users/alganet', function(user) {
alert(user.name);
});
您应该使用不同的操作相应的HTTP方法。保存的用户会是这样的:
$router->post('/users/*', function($userName) {
return MyDatabaseLayer::saveUser($_POST['user']); //Illustrative
});
的jQuery:
$.post('/users', $("$userform").serialize());
主要有四种HTTP方法:GET,POST,PUT和DELETE。 GET和POST是最常见的。
有一个很好的琐事:HTTP,REST和AMF都是由同一个人写的:Roy Fielding。
注意安全隐患:防止JavaScript(通过Jonathan Sampson接受的答案中的某些代码访问PHP)能够访问不需要的方法。 – koen 2012-04-28 17:34:50