我有一个json_data.php
文件,我想在使用getJSON
的switch语句中传递一个参数给函数。
<?php
require_once "Db.php";
$db = new Db();
if(isset($_GET['method'])) {
switch (($_GET['method'])) {
case 'getUserIds':
echo json_encode($db -> getUserIds());
exit();
case 'getUser':
echo json_encode($db -> getUser($userId) {
exit();
// other cases go here
case 'getCertCategories':
echo json_encode($db -> getCertCategories());
exit();
case 'get
default:
break;
}
}
如何通过参数将$userId
传递给getUser
函数?
$.getJSON
的第二个参数是一个包含查询参数的对象。
$.getJSON('json_data.php', { method: 'getUserIds' }, function(response) {
...
});
$.getJSON('json_data.php', { method: 'getUser', userId: 'joe' }, function(response) {
...
});
在PHP中,您将访问$_GET['method']
, $_GET['userId']
等