繁体   English   中英

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

[英]How to pass parameters to php using get JSON

我有一个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']等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM