如何使用get JSON将参数传递给php

乔纳森·基特尔(Jonathan Kittell)

我有一个json_data.php文件,我想使用将参数传递给switch语句中的函数getJSON

<?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;
    }

 }

如何传递参数以便传递$userIdgetUser函数?

巴尔玛

的第二个参数$.getJSON是一个包含查询参数的对象。

$.getJSON('json_data.php', { method: 'getUserIds' }, function(response) {
    ...
});

$.getJSON('json_data.php', { method: 'getUser', userId: 'joe' }, function(response) {
    ...
});

在PHP中,你将访问$_GET['method']$_GET['userId']等等。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章